Connecting Tech Pros Worldwide Help | Site Map

setting global variables in javascript from html.

  #1  
Old August 31st, 2008, 12:53 AM
Newbie
 
Join Date: Aug 2008
Posts: 2
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.

Last edited by gits; August 31st, 2008 at 11:09 AM. Reason: added code tags
  #2  
Old August 31st, 2008, 03:20 AM
Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Posts: 102

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.
  #3  
Old September 1st, 2008, 08:29 AM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 341

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
  #4  
Old September 1st, 2008, 09:10 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,521
Provided Answers: 12

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 Threads
Thread Thread Starter Forum Replies Last Post
setting variables in outer functions Tommy Nordgren answers 18 November 4th, 2007 01:55 PM
Variable Scope in JavaScript pbmods insights 1 July 26th, 2007 06:52 PM
Problem in Event Handling sorobor answers 2 July 11th, 2007 01:01 PM