Click to See Complete Forum and Search --> : Lifetime of variables issue


jovialjonny
06-04-2003, 09:47 AM
I'm going nuts from this one!
I have a function that takes a string from a form, what I want to do is store this string in a global variable like so:

var url;
function storeUrl(theUrl)
{ url = theUrl;
}

What happens next is that in a child window there is an onLoad function that calls back to the parent to get this url value. (the child page needs to be fully loaded before it takes the variable so this seemed to be the best way to achieve this) I've implemented some suggestions I got from this forum about this matter and right now the code is like this:

this is called in child window onLoad:
function loadUrl()
{ var url = window.opener.getUrl();
addToViewer(url);
}

in parent window:
function getUrl()
{ return url;
}

But by the time the call is made the url is coming back as undefined, I think this is because the variable is destroyed before the child window calls for it.

Does anyone know how I could make this work?

Khalid Ali
06-04-2003, 09:55 AM
You could have kept asking question in the last thread as well..Now to your question..here is your function that opens the window....

var viewer = false;
var url;
function callViewer(theUrl){
if(viewer ==false ){ window.open("multiviewer.html", "mv");
viewer =true;
}
url = theUrl)<----Extra parenthesis
}

BTW now that I lookat your function you have an extra parenthesise
What I'd suggest is set the url first

url = theUrl;
if(viewer ==false ){ window.open("multiviewer.html", "mv");
viewer =true;
}

jovialjonny
06-04-2003, 10:21 AM
Sorry for blabbin out new threads...

Anyway there are still two problems here and I'm sure I'm doing something silly somewhere so I'm just gonna show you exactly what's going on.
The 2 problems are:
1: the 2nd time I click the button the viewer is still saying its false (my guess is its somehow being re-initialised)

2: The url is passing but only when the alert that I have marked out below is included (which I was using to test it), if I take out that alert, the url is lost.

Layout is:
HTML page containing form for entering a url:
<body>
<form onsubmit="callViewer(urlEntry.value)" >
Please enter a url:<input type="Text" name="urlEntry" value="http://www.google.com"/>
<input type="Submit" value="Subscribe" />
</form>
</body>

This is linked to this javascript file:
var viewer = false;
var url;
function callViewer(theUrl)
{ url = theUrl;
if(!viewer)
{ window.open("multiviewer.html", "mv");
viewer =true;
}
alert("in callViewer:"+url); /**URL IS UNDEFINED IF I TAKE OUT THIS ALERT**/
}
function getUrl()
{ return url;
}

In the child page onLoad this function is called:
function loadUrl()
{ var url = window.opener.getUrl();
addToViewer(url);
}

At one stage I had the variable 'viewer' being a reference to the child window but that didn't work either.
This is a bit like a crossword I have been looking at too long, I just cant figure out what's wrong:(

Khalid Ali
06-04-2003, 11:00 AM
Here is what I have and it shows the url in the text field reading from the parent window

http://68.145.35.86/temp/PopupProblem_jovialjonny.html

jovialjonny
06-04-2003, 12:02 PM
Still no joy! If I take that alert out it still won't work but the main problem is it is never skipping the 'if' statement:
function callViewer(theUrl)
{ url = theUrl;
alert("before if:"+viewer);
if(!viewer)
{ window.open("http://iss-rose.ibi.com/dev/multiviewer.html", "mv");
viewer =true;
}
alert("after if:"+viewer);
}

The 'before if' alert is always producing false, after the first time it should be true. The page I am opening is building a tab based system for flicking between web pages so its vital this re-opening does not occur or there will only be one tab every time.

Khalid Ali
06-04-2003, 12:07 PM
Hold on here....
Wasn't your concern first was that you could not access the url's value oin the poup?

Now it seems like you are talking about viewer variables value being false or true...can you be a bit clear in saying that what exactly it is you want and what is it that is not happening..

And BTW if you had seen the link I posted..if you open the popup and then close it,and try to open it again ..it will not open that means the viewer is being read is true or false correctly..

jovialjonny
06-06-2003, 08:18 AM
Originally posted by Khalid Ali
Hold on here....
Wasn't your concern first was that you could not access the url's value oin the poup?

Now it seems like you are talking about viewer variables value being false or true...can you be a bit clear in saying that what exactly it is you want and what is it that is not happening..

And BTW if you had seen the link I posted..if you open the popup and then close it,and try to open it again ..it will not open that means the viewer is being read is true or false correctly..

I have looked at your link and it was helpful, thanks for that.
The thing is there are a few problems going on, I was just trying to break them down a bit.

Here is the main issue I have right now:
I have the parent window which opens the child window and gives it the url- this works okay now.

The problem is that (with both windows now open) the second time the user clicks 'subscribe' to give a new url I DO NOT want the parent to re-open the child window, all that should happen is that the parent should call the child's 'addToViewer(theUrl)' method and drop the new url in there. The child page's javascript handles the rest.

Right now I cannot stop the reload from occurring and I don't know why. This is the code:

var viewer = false;
var url;

function callViewer(theUrl)
{ url = theUrl;
alert("before if:"+viewer); ***
if(!viewer)
{ viewer =true;
window.open("multiviewer.html", "mv");
}
alert("after if:"+viewer);
}

Right now the alert I marked says that viewer is always false. I thought that after the window has been opened the first time it should be true but I cannot get it to skip this 'if' statement when the child window is open so it keeps reloading.

Please let me know if this is a bit clearer and if you have any idea why viewer keeps being reset to false on subsequent calls to this function. thanks

jovialjonny
06-06-2003, 08:30 AM
Right I've tracked down the problem...
after the user clicks the subscribe button the parent window loads the child and then reloads itself, thereby resetting the 'viewer' variable to false.

Do you know how I can stop this reload from occurring?

Khalid Ali
06-06-2003, 09:16 AM
I think I know what ae you talking about ..check the link posted above,,,,I have updated the scripts..

jovialjonny
06-06-2003, 09:25 AM
Originally posted by Khalid Ali
I think I know what ae you talking about ..check the link posted above,,,,I have updated the scripts..

Emmm parden my noob-ness but I'm not seeing a difference in the new scripts though I do see that your example doesn't reload which is what I am looking for.

Can you tell me what the difference is?

Khalid Ali
06-06-2003, 09:33 AM
Actually code wise there is a pretty good difference..in that now I am using a reference to the popup window that is being opened and then I am checking that if the referenced child window is closed or open.if its open then I am passing the url value from the form of the parent to the form of the child window.
Instead of earlier where you were using a boolean flag....

jovialjonny
06-06-2003, 09:46 AM
Are you sure the link you have up above is to the new scripts? I'm not seeing those differences. I just grabbed this source from your page:

var viewer = false;
var url="";
function callViewer(theUrl){
url = theUrl;
if(!viewer){
window.open("multiviewer.html", "mv",'width=300,height=200,status=yes');
viewer =true;
}
}

And its still using a boolean flag.

Khalid Ali
06-06-2003, 09:48 AM
LOL..you must be using IE..it has a crappy habit of caching everything..

http://68.145.35.86/temp/PopupProblem_jovialjonny.html

try reloading /refreshing few times

jovialjonny
06-06-2003, 09:59 AM
Eh I've reloaded, refreshed, deleted temp files, done just about everything and I'm still not seeing it. I have had caching issues with IE before but one of the above usually takes care of it.

Could u just paste the code into a reply or something please?

Khalid Ali
06-06-2003, 10:01 AM
Most certainly...

here it goes


var popupWin = null;
var url="";
function callViewer(theUrl){
url = theUrl;
if(popupWin==null || popupWin.closed==true){
popupWin = window.open("multiviewer.html", "mv",'width=300,height=200,status=yes');
}else{
popupWin.document.getElementById("form1").t1.value = document.form1.urlEntry.value;
}
}



All other code is same

jovialjonny
06-06-2003, 10:30 AM
Ya thats definitely gonna work better but the same issue is still there: when I click the button the parent is reloading, causing popUpWindow to re-initialise to null. I have no idea why this is happening but it is a nightmare!

Is there some way to check if a window exists by its name, so I would not be reliant on the use of global variables i.e after calling:
window.open("multiviewer.html", "mv");

Can I just check to see if "mv" exists the second time around. I don't know if this is clear but basically in a perfect world I would want something like this:
if(window.exists("mv"))
.....

is there anything like this in JavaScript?

Khalid Ali
06-06-2003, 10:34 AM
look at the code

this condition

if(popUpWindow==null || popUpWindow .closed

does precisely that.

How come you can not test the code in netscape6+ brosers..because its annoying to some extent that it doesn't work..where as I know it works...
Do something about your browsers caching problem....use some other browser or something my friend.
Because code works..I have tested it for NS6+/IE6+

jovialjonny
06-06-2003, 10:38 AM
I just spotted that I am using the submit method and onSubmit for the button whereas you are using onClick. I think that might be why it is reloading but I can't change that because I will be submitting data to a servlet later on in the system's development.

Plus I am plugging into a system that will be using submit and I can't just use onClick. Any idea how to solve this frustrating thing??

Khalid Ali
06-06-2003, 10:42 AM
See now you are talking..
lol


how are you calling callViewer method..I want to see you r form elements code and sybmit buttons code

jovialjonny
06-06-2003, 11:05 AM
Originally posted by Khalid Ali
See now you are talking..
lol


how are you calling callViewer method..I want to see you r form elements code and sybmit buttons code

In accordance with the marvellous world of computing I just met with my supervisor who has told me that things have changed and its going to work differently now, in a way I actually understand and can hopefully write without too much drama!

Thanks a million for all your help with this, I would have totally lost my mind otherwise in the past few days! Something tells me I will be back again soon but maybe I can have a day or two now without hitting any brick walls!:)

Khalid Ali
06-06-2003, 12:17 PM
Originally posted by jovialjonny
I can have a day or two now without hitting any brick walls!:)

We don't want you do do that now, do we?
:D