Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-06-2011, 09:30 PM   PM User | #1
slurpey
New to the CF scene

 
Join Date: May 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
slurpey is an unknown quantity at this point
How to pass along a url parameter to any links clicked?

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
Code:
<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
Code:
<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
Code:
<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!
slurpey is offline   Reply With Quote
Old 07-07-2011, 12:34 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, the general way you get the value of a parameter is this:

Code:
<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
Code:
     pageParams["endpage"]
(as an example).

So you could, for example, create the <A> tags you wanted thus:
Code:
<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.
Old Pedant is offline   Reply With Quote
Old 07-11-2011, 06:11 PM   PM User | #3
slurpey
New to the CF scene

 
Join Date: May 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
slurpey is an unknown quantity at this point
Thanks!

1000 thanks. It's exactly what I was looking for. Works perfectly. Once again, thank you.
slurpey is offline   Reply With Quote
Reply

Bookmarks

Tags
parameters

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:15 AM.


Advertisement
Log in to turn off these ads.