I got a problem with the "onload-event handler" of "window". This is the important part of my code:
var name = "jack";
var newWin = window.open("assets/new.html", ... , ...);
newWin.onload = function() { testFunction(name); }
It is supposed to open a new window, load some html-file into it and AFTER completion execute a function named "testFunction(text)" defined in the same script.
But the console all the time says "testFunction not defined".
I have also tried to do a call to "window.opener.testFunction(window.opener.name);" within the "onload-function". But it doesnt work either :/
I'm so frustrated, because I tried for hours by now. What is the problem with my script?
Thanks in advance!
Ultimater
05-03-2006, 02:22 PM
newWin.onload=function(){newWin.testFunction(name)}
Because the above is interchangeable with the following:
function myFunction(){
newWin.testFunction(name)
}
newWin.onload=myFunction;
rapthor
05-05-2006, 12:20 PM
var name = "jack";
var newWin = window.open("assets/new.html", ... , ...);
newWin.onload = function() { newWin.testFunction(name); }
Okay this works! But it does not work every time! Sometimes, the command window.open is faster than specifying the onload function in the next line. So the result is, that the function in onload is NEVER called. :mad:
So, I could simply put in an additional line like this "newWin.testFunction(name);". But as you know, if it is the case, that window.open is very slow, this new line would give me the error, that the function "testFunction" in newWin isn't defined. Because the opened window didn't build completely.
So how do I manage those two cases? In either one I want to call the function in the new opened window.
Thanks again.
Ultimater
05-05-2006, 12:38 PM
Untested:
var name = "jack";
var newWin = window.open("assets/new.html", ... , ...);
var wasCalled=false;
newWin.onload = callFunc;
setTimeout(callFunc,100);
function callFunc(){
if(wasCalled)return;wasCalled=true;
if(!newWin.testFunction){wasCalled=false;return;}
mainFunc()
}
function mainFunc(){
newWin.testFunction(name)
}
rapthor
05-06-2006, 05:14 AM
Hi,
thanks again but what do you intend to achieve by using "if(!newWin.testFunction){wasCalled=false;return;}"? I think I'm unsure about the effect of that. What does it mean to call "newWin.testFunction" without brackets?
Ultimater
05-07-2006, 03:07 AM
if(newWin.testFunction)
without parenthesis in an if-statement is a test if the function exists but doesn't actually execute the function. For example:
if(window.aFunctionNotDefined){
alert("The function aFunctionNotDefined exists")
}
else{
alert("The function aFunctionNotDefined does not exist")
}
</script>
newWin.testFunction, if it is defined, would return the code which defines it.
<script type="text/javascript">
function hi(){
alert("If you can read this, the function hi had been called.")
}
alert(hi); //alerts the code that defines the function hi.
</script>
Since a "true" boolean value is anything which isn't 0, a blank string, null, or false,
a filled string, a number other than 0, the code defining a function, or an object would simulate and be treated as a "true" boolean value.
The intention of the code in my previous post was to be sure that the "mainFunc" function executes once, only once, at least once. Once it should be called and to call the "testFunction" function not before it exists, but only after it is defined in the pop up window.
If the ONLOAD event fired prior to assigning a function to it, your testFunction function wouldn't be called. So in case the ONLOAD event already fired, I had to give the testFunction function a call manually. The question then becomes "How does one test whether the ONLOAD event was fired yet?" My approach was to use a global variable "wasCalled" and initialize it to false. If the ONLOAD event fires before the 100 milliseconds elapse, the variable "wasCalled" is set to "true" so the setTimeout doesn't execute the "mainFunc" twice. If the setTimeout fires before the ONLOAD, it first checks to see if the function "testFunction" in the pop up window is defined yet. If it isn't defined, surely the ONLOAD event didn't fire. Thus, the function "testFunction" at this point in time still doesn't exist nor did the ONLOAD fuction fire. That is a good sign. So we wait for the ONLOAD to execute the mainFunc". To do this we set "wasCalled" to "false" so when the ONLOAD will later fire, it will execute the "newWin.testFunction(name)" as usual.
If this is still not 100% clear to you, please tell me what to elaborate-on so I can give some more example code.
rapthor
05-09-2006, 12:47 PM
Okay, it's clear now :) thanks!
I got another problem now. The code you mentioned works fine, if i Put it in a function like this:
function start()
{
var name = "jack";
var newWin = window.open("assets/new.html", ... , ...);
var wasCalled=false;
newWin.onload = callFunc;
setTimeout(callFunc,100);
function callFunc(){
if(wasCalled)return;wasCalled=true;
if(!newWin.testFunction){wasCalled=false;return;}
mainFunc();
}
function mainFunc(){
newWin.testFunction(name);
}
}
Exactly the same code placing into antoher function does not work. This function is looking similar to this one:
function anotherOne()
{
...
...
var sure = confirm("Really execute?");
if (sure)
{
var name = "jack";
var newWin = window.open("assets/new.html", ... , ...);
var wasCalled=false;
newWin.onload = callFunc;
setTimeout(callFunc,100);
function callFunc(){
if(wasCalled)return;wasCalled=true;
if(!newWin.testFunction){wasCalled=false;return;}
mainFunc();
}
function mainFunc(){
newWin.testFunction(name);
}
}
else
......
}
So the only "big" difference is, using your code inside an if-construction, so it seems.
The error I get is "callFunc is not defined" on this line:
"newWin.onload = callFunc;".
So I thought placing the definitions of the two inline functions BEFORE that onload-assignment will fix it. But after changing the order I get another message: "name has no properties". This is the line mentioned within the inline-function called "function mainFunc()":
"newWin.testFunction(name);".
This means "name" is undefined (Firefox uses this error message).
I don't get, why the same code is working above, but not in the second case!
Can you help me again, please? :)
Ultimater
05-09-2006, 01:03 PM
The code you mentioned works fine, if i Put it in a function like this:
function start()
{
var name = "jack";
var newWin = window.open("assets/new.html", ... , ...);
var wasCalled=false;
newWin.onload = callFunc;
setTimeout(callFunc,100);
function callFunc(){
if(wasCalled)return;wasCalled=true;
if(!newWin.testFunction){wasCalled=false;return;}
mainFunc();
}
function mainFunc(){
newWin.testFunction(name);
}
}
The code is not intended to be used like that, even if it does work.
mainFunc and callFunc should be declared globally -- not within another function.
In otherwords:
<script type="text/javascript">
function callFunc(){
if(wasCalled)return;wasCalled=true;
if(!newWin.testFunction){wasCalled=false;return;}
mainFunc();
}
function mainFunc(){
newWin.testFunction(name);
}
function start()
{
name = "jack";
newWin = window.open("assets/new.html", ... , ...);
wasCalled=false;
newWin.onload = callFunc;
setTimeout(callFunc,100);
}
</script>
Also note that I have removed all "var" instances so as-to declare the needed varaibles globally and not locally so that other functions can access them.
Assuming you first code worked and your 2nd code didn't, I believe the reason is because of the time delay of the user hitting "OK" on the confirm.
To verify if this is the reason, try replacing:
var sure = confirm("Really execute?");
with:
var sure = true;
If it still gives an error, maybe your initial first code didn't work either -- like I'd expect (seeing as the functions and variables should be defined globally).
rapthor
05-09-2006, 01:47 PM
Ah, well I understood. But may it be possible that the timeout set to 100 ms is too short to work on slow http connections? Assuming the new window contains many lines of HTML code, the "onload"-event would be fired later.
Assuming you first code worked and your 2nd code didn't, I believe the reason is because of the time delay of the user hitting "OK" on the confirm.
Ultimater
05-09-2006, 01:52 PM
The function that the setTimeout calls needs to be declared globally.
rapthor
05-09-2006, 02:00 PM
I will test that.
I'm glad to have a user like you, that is concerning with my "problems". Thanks a lot!
rapthor
05-10-2006, 04:40 AM
Hm ... it is mysterious somehow.
Although making the functions and variables global, it does not seem to work for the second case.
May the reason be using "window.open();" in an AJAX response function? (It is the function called, when the response of the request is going in.)
// global definitions for onload variables
var name = "jack";
var newWin;
var funcWasCalled = false;
// here is window.open()
function getAJAXRequestResult() {
if (req.readyState != 4)
return;
......
......
var showSummary = confirm("yes or no");
if (showSummary)
{
funcWasCalled = false;
newWin = window.open("packing.html", "", "", 660, 700);
setTimeout(callFunc,100);
newWin = callFunc;
}
}
// global definitions for the onload functions
function callFunc()
{
if(funcWasCalled)
return;
funcWasCalled=true;
if(!newWin.testFunction)
{
funcWasCalled=false;
return;
}
mainFunc();
}
function mainFunc()
{
newWin.testFunction(name);
}
The error I get is "funcWasCalled is not defined"! At this line in function "callFunc()":
if(funcWasCalled)
return;
As you can see, this variable is global ... so where's the mistake?
This does not resolve it. In fact I'm using req.send(encodeURI("parameter=" + xyz)); instead of req.send(null). (Just to give you a short summary, I thought I will cut that out).
and changing:
if (req.readyState != 4)return;
to:
if (req.readyState != 4 ||req.status != 200)return;
If I use that, my "getAJAXRequestResult;" is always returning and never processing completely.
Unfortunately I have to specify the "Content-Type" like "application/x-www-form-urlencoded; charset=UTF-8 ". In my application it is not req.send(null) but req.send(encodeURI("parameter=xyz"));.
If I leave out this customized content-type, the server does not receive any parameters.
It was quite puzzling to me to find that solution, because before a few weeks I had to use Ethereal to spy ethernet traffic in order to see what content type is sent, when using standard HTML-FORMs. I just copied that content-type and put it in there: req.setRequestHeader("Content-Type", requestheader);. From then on, it worked fine ...
and changing:
req.open ("POST", "someURL.htm", true);
to a GET request:
req.open ("GET", "someURL.htm", true);
I assume you are using Ajax because you plan to later make use of req.responseText. which I don't see anywhere
Using "GET" instead of "POST" isn't possible because I get the same error as if I would leave out the content-type -> the submitted parameter is not recognized.
You were right: req.responseText is used later on. I left that part out of the posted code to give a short example of what I mean.
So, finally the problem of opening a new window and dynamically extending its content remains. There is still that error saying "funcWasCalled is not defined".
Just to give you the code I'm using now:
function doAJAXRequest()
{
.....
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.overrideMimeType('text/xml');
}
if (req.readyState != 4)
return;
......
// the window.open(...)-thing
......
}
Ultimater
05-11-2006, 02:53 PM
and changing:
if (req.readyState != 4)return;
to:
if (req.readyState != 4 ||req.status != 200)return;
If I use that, my "getAJAXRequestResult;" is always returning and never processing completely.
A status of 200 means the request was ok and if the status is not 200, then there was a problem in the request.
I suspect that the POST data is corrupted.
Try
req.send("requestXML="+encodeURI(xml));
req.open ("POST", "someTomcatURL", true);
Is whatever you have for "someTomcatURL" correct? It must be located on the same domain. (i.e. without resorting to privilege enabling)
rapthor
05-11-2006, 03:46 PM
A status of 200 means the request was ok and if the status is not 400, then there was a problem in the request.
I suspect that the POST data is corrupted.
Try
req.send("requestXML="+encodeURI(xml));
Okay, I will try this on monday because this is the next time I've the chance to do so.
Isn't it supposed to be like "req.send(encodeURI("requestXML="+xml));" instead of "req.send("requestXML="+encodeURI(xml));"?
Is whatever you have for "someTomcatURL" correct? It must be located on the same domain. (i.e. without resorting to privilege enabling)
It's a URL mapped to a servlet (so it does not matter how you name it, due to you can map any path to any controller). In detail, it is the spring dispatcher servlet (Spring Application Framework (http://en.wikipedia.org/wiki/Spring_framework)) at first glance. From there on the request is forwarded to another servlet depending on the URL. Every servlet is in the same domain and on the same server.
Thank you so much and have a nice weekend .. I will have another try on monday.
Ultimater
05-11-2006, 08:49 PM
For debudding purposes, try this:
function getAJAXRequestResult()
{
if (req.readyState != 4)return;//0:uninitialized, 1:loading, 2:loaded, 3:interactive, 4:complete