473,569 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setting global variables in javascript from html.

2 New Member
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="javas cript" src="test.js"></script>
</head>
<body>
<i><input type="text" onclick="this.v alue = ''" id="machine" value="Enter Node Name!" size="36" name="machine"> </i>
<a onclick="javasc ript:goSubmit() " href="other.htm l" 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.
Aug 30 '08 #1
3 2446
Ferris
101 New Member
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.
Aug 31 '08 #2
RamananKalirajan
608 Contributor
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="JavaS cript">

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
Sep 1 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
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.
Sep 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
24165
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
2
1692
by: lawrence | last post by:
I thought I was fighting a PHP problem, so I asked about this on a PHP list, but they felt it was a Javascript problem, so I'm reposting a bit of the debate here. Could this Javascript, below, possibly trigger a redirect in Netscape, and send the browser to a file called "0". Cause what is happening is on load, in Netscape, the page redirects...
8
2520
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables...
8
9696
by: David McDivitt | last post by:
I need to set tabs on java generated pages. Pages have four sections: header, sidebar, body, and footer. The sidebar and body change dynamically. The tab key must go to anchors, fields, and buttons doing all in the header first, all in the sidebar second, etc. A base page contains includes for all the pieces and has the body tag. I am...
1
6462
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.