469,903 Members | 1,736 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

var from one HTML file to another

In JavaScript, how do you pass a var from one HTML file to another
one? I "own" both of these HTML files.

Scott
Jul 23 '05 #1
3 906
Scottie wrote:
In JavaScript, how do you pass a var from one HTML file to another
one? I "own" both of these HTML files.


Cookies, parsing the query string, frames, some server side language reading
POST, GET or the cookies and writing a <script> element dynamically. There
are several options.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Jul 23 '05 #2
David,

What I did was:
var str = window.location.search;
if (str != "") {
var temp = str.split('=');
alert(temp[1]);
}
else {
alert("This is NULL");
}

and it works. Thanks.

Scott
David Dorward <do*****@yahoo.com> wrote in message news:<c8*******************@news.demon.co.uk>...
Scottie wrote:
In JavaScript, how do you pass a var from one HTML file to another
one? I "own" both of these HTML files.


Cookies, parsing the query string, frames, some server side language reading
POST, GET or the cookies and writing a <script> element dynamically. There
are several options.

Jul 23 '05 #3
Scottie wrote:
David,

What I did was:
var str = window.location.search;
if (str != "") {
var temp = str.split('=');
alert(temp[1]);
What if str.split('=') doesn't create an array, or doesn't create more then one element in the array?
What if there are multiple items being passed on the URL? You'll get both the first value and all the
remaining text from window.location.search.

}
else {
alert("This is NULL");
No, it's not null, because that's not what you're testing str against. Your condition says [if (str !=
"") {], so the else branch of that condition is that (str == ""). In fact, if str is null, it will take
the positive branch (because (null != ""), which will result in an error when you attempt to do
str.split().
}

and it works. Thanks.

Scott


Here is a much better solution:

var s = window.location.search;
// set s for testing
s = 'a=b&c=d&YourAttribute=MyValue&e=f&g=h';

// find the exact value associated with your attribute
(new RegExp('YourAttribute=([^&]+)')).test(s);
if (RegExp.$1) {
alert(RegExp.$1);
} else {
alert('There is no value for "YourAttribute"');
}

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Dave Smithz | last post: by
1 post views Thread by Tarlanim | last post: by
4 posts views Thread by Neil.Smith | last post: by
42 posts views Thread by Santander | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.