Connecting Tech Pros Worldwide Help | Site Map

setting global variables in javascript from html.

Newbie
 
Join Date: Aug 2008
Posts: 2
#1: Aug 31 '08
hello all,
please forgive me if this is stupid question as my knowledge of programming is limited and thanks in advance for the answers.
i have an external javascript file test.js and it has

test.js
-------------------------
Expand|Select|Wrap|Line Numbers
  1. var x = null;
  2. var y = null;
  3. function goSubmit()
  4. {
  5.    x = document.getElementById("machine").value;
  6.   alert(x+"inside");
  7. }
  8.  
  9. function calling()
  10. {
  11. y = x;
  12. alert(y+"outside");
  13. ....somecode
  14. }
and i have html page test.html with following code
[HTML]<html>
<head>
<script language="javascript" src="test.js"></script>
</head>
<body>
<i><input type="text" onclick="this.value = ''" id="machine" value="Enter Node Name!" size="36" name="machine"></i>
<a onclick="javascript:goSubmit()" href="other.html" target="_self">Connect</a>
</body>
</html>[/HTML]

and the result i get when i input the name "system1" i got the following reults
system1 inside
null outside.

so the question is there any work around possible to assign value to global variable so that they can be used later on in the script. some coding example (if possible making the above script complete) will be highly appreciated.
Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Posts: 102
#2: Aug 31 '08

re: setting global variables in javascript from html.


Did you call the function "calling" in Other.html ? If you want to pass variable "x" to Other.html , you should use URL parameters. test.js will be initialized when Other.html is loaded , and then variable x will be null.
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#3: Sep 1 '08

re: setting global variables in javascript from html.


Hi, for your problem each and every page have their own instances. You can not refer the same variable in two pages. Thats why you are getting null in the "other.html". there must be some other way to do as per your requirement. I dont know that (i.e. using global variable). but u can use form variables and through URL. it will be use to retrieve the value from one page to other. here goes an example for that

Test.html
[HTML]
<html>
<form method="get" action="others.html">
System Name: <input type="text" name="node" id="node">
<br/>
<input type="submit" value="submit">
</form>
</html>[/HTML]

Others.html
[HTML]<html>
<script language="JavaScript">

function doThat()
{
var val = location.search;
var x = new Array();
x = val.split("=");
alert(x[1]);
}

</script>
<body onload="doThat();">
</body>
</html>[/HTML]

Hope this may be helpful for u.

Regards
Ramanan Kalirajan
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Sep 1 '08

re: setting global variables in javascript from html.


Another alternative is if you use window.opener to open a pop-up window, you can use window.opener.x to refer to the variable in the parent window.
Reply


Similar JavaScript / Ajax / DHTML bytes