PDA

View Full Version : passing variable between functions


homerUK
12-21-2002, 12:30 PM
hi,

I have one main page called "index.htm" -- this has a Javascript file called "functions.js". There are buttons on the index page which popup different windows depending on which button you press.

I have a function called "isForm" which detects if there is a valid "form" tag within the page. In this function, I need to set a variable which can be passed on to the popup window....... here's the code:

function isForm()
{

if (iView.document.selection.type == "Control")
{
//the user must be selecting to MODIFY the form element, so open the window for modifying
alert("you want to modify it");
return true;
}

else

{

//there is a form, so carry on to see if the user has placed the cursor INSIDE the form tags

iView.focus();
var cursorPos = iView.document.selection.createRange();
var elt = cursorPos.parentElement();
if (elt.tagName == "FORM") {
return true;
} else {
alert("You are not inside the form, or there is no form found");
return false;
}
}


}


that checks if the cursor is inside the form tag..... then I use the following code to open the popup window...


function formTextfield()
{
if (isForm()) {
imageWin = window.open('editor/files/form_textfield.htm','','width=500,height=300,scrollbars=yes,resizable=yes,titlebar=1');
}
}

so that function uses "isForm" to check if it's a form, and opens the popup depending on if it is in the form or not.
but in the code above I do a check to see if the user has selected a Control element... this is because if they have already inserted a form field, they must be wanting to edit it.
I thought I could just do

var todo = "modify";

then in the popup window use

if (window.opener.todo == "modify")
//start the modify code

but it doesnt get the "todo" value.

Any ideas??

thanks..!!

ScottInTexas
12-21-2002, 01:13 PM
If I understand you correctly you need a "global" variable. You can create the variable just inside the SCRIPT tag before any functions and all the functions below it can use the variable.

Algorithm
12-21-2002, 09:59 PM
Well, if nothing else, you could always pass this value in the URL of the page in the opened window, such as:

window.open('editor/files/form_textfield.htm?modify=true');

You could then check against the opened window's location.search property.

homerUK
12-22-2002, 12:22 AM
hey guys... cheers for the responses...

I think the easiest way would be to pass the variable over the URL.....

I could do something like:


IF (todo == "modify")
{
window.open('editor/files/form_textfield.htm?modify=true');
}
else
{
window.open('editor/files/form_textfield.htm?modify=false');
}


then in the open window, I would say something like:

var todo = location.search.modify;

??? would that get the value of modify.......???

thanks!! :thumbsup:

requestcode
12-22-2002, 12:30 AM
Check out this link to a tutorial on the subject:
http://www.javascriptkit.com/javatutors/send1.shtml

homerUK
12-22-2002, 01:02 AM
cheers for the URL.... I have had a look, and it all makes sense... but I think I need to declare a global variable....

is this done by simply doing

var todo = "modify";

and will that be available in every function??

mhere
12-22-2002, 10:36 AM
yes, it will be accesible and changable in every function