Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 31st, 2008, 12:53 AM
Newbie
 
Join Date: Aug 2008
Posts: 2
Default setting global variables in javascript from html.

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
Reply
  #2  
Old August 31st, 2008, 03:20 AM
Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Age: 22
Posts: 102
Default

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.
Reply
  #3  
Old September 1st, 2008, 08:29 AM
RamananKalirajan's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Chennai - India
Age: 24
Posts: 236
Default

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

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
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles