using variable from javascript function

J

jon

I would like to use the variable that the javascript creates as it
parses the URL and be able to use this variable in other links on the
same page. notice how link to page 2 has the token but the other links
do not. Can I have one javascript in the heading and then call it up
multiple times in the html to make my links (i.e.
page3.html?refer=google) I have the following webpage.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1">
<title>Untitled Page</title>
<script language="JavaScript">
//<![CDATA[
function parseSearchString()
{
var pairs=unescape(location.search.substring(1).replace(/\+/g,"
")).split('&');
for (var i=0;i<pairs.length;i++){
var pair = pairs.split('=');
this[pair[0]]=pair[1];
}
}
var search = new parseSearchString();

//this will output all the passed variables

for (o in search)
document.write('<a href="page2.html?refer=' + search[o]
+'">Page2</a>');
</script></head>

<body bgcolor="#ffffff">
<p></p>
</body>

</html>

Thanks.

Jon Lanclos
(e-mail address removed)
 
M

Michael Schmitt

Hi jon,
I tested your script and it worked well, but keep in mind that you overwrite
the content of your page, if you use document.write().
I would like to use the variable that the javascript creates as it
parses the URL and be able to use this variable in other links on the
same page. notice how link to page 2 has the token but the other links
do not. Can I have one javascript in the heading and then call it up
multiple times in the html to make my links (i.e.
page3.html?refer=google) I have the following webpage.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type"
content="text/html;charset=iso-8859-1">
<title>Untitled Page</title>
<script language="JavaScript">
//<![CDATA[
function parseSearchString()
{
var pairs=unescape(location.search.substring(1).replace(/\+/g,"
")).split('&');
for (var i=0;i<pairs.length;i++){
var pair = pairs.split('=');
this[pair[0]]=pair[1];
}
}

var search = new parseSearchString();

//this will output all the passed variables

for (o in search)
document.write('<a href="page2.html?refer=' + search[o]
+'">Page2</a>');


function createLinks() {
var search = new parseSearchString();
var b = document.getElementsByTagName("body")[0];
for (o in search) {
var a = document.createElement("a");
a.href = 'page2.html?refer=' + search[o];
var t = document.createTextNode("This links to page2.html with the post variable refer set to: " + search[o]);
a.appendChild(t);
b.appendChild(a);
var br = document.createElement("br");
b.appendChild(br);
}
}

</script></head>

<body bgcolor="#ffffff">

and here we call the function to set up the links:
<body style="background-color:#ffffff" onload="createLinks();"
<p></p>
</body>

</html>

Thanks.

Jon Lanclos
(e-mail address removed)

Tested with Mozilla 1.7 and IE 6.0 Sp1
Of cause you can append your links to other elements too.

hth, Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top