...

View Full Version : How to pass along a url parameter to any links clicked?



slurpey
07-06-2011, 09:30 PM
Hi, I have an "offline" html demo of 50+page - so it needs to run on standard html/javascripts...

I'm trying to do this:

Page A

<a href="pageb.html?endpage=pageD.html">Page B.html</a>
<a href="pageb.html?endpage=pageE.html">Page B.html</a>
<a href="pageb.html?endpage=pageF.html">Page B.html</a>

Page B

<a href="PageC.html?endpage=(the parameter of page A)">Page C</a>
<a href="(the parameter of page A)">End Page</a>

Page C

<a href="(the parameter of page A)">End Page</a>

I've searched for a couple of hours already and I just can't seem to find this... I'm not that great at javascripts:(... Thanks a lot!

Old Pedant
07-07-2011, 12:34 AM
Well, the general way you get the value of a parameter is this:



<script type="text/javascript">
var pageParams = [];
if ( location.search.length > 1 )
{
var pairs = location.search.substring(1).split("&");
for ( var p = 0; p < pairs.length; ++p )
{
var pair = pairs[p].split("=");
if ( pair.length > 0 )
{
pageParams[ pair[0] ] = decodeURIComponent( pair[1] );
}
}
}
</script>

Now, any time you need that value of a querystring parameter, you just code

pageParams["endpage"]

(as an example).

So you could, for example, create the <A> tags you wanted thus:


<script type="text/javascript">
document.write('<a href="PageC.html?endpage=' + pageParams["endpage"] + '">Page C</a>');
...
document.write('<a href="' + pageParams["endpage"] + '">End Page</a>');
...
</script>

But there are other ways to do the equivalent, using DOM modifications instead of document.write.

slurpey
07-11-2011, 06:11 PM
1000 thanks. It's exactly what I was looking for. Works perfectly. Once again, thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum