简体   繁体   中英

jquery: How to dynamically pass URL parameters to a link in the page

I want that the outbound and iframed links on my site should append the url parameters from the url of the current page.

So, lets say I have a page mysite.com and I have a link on that page that leads to somesite.com (or I have iframed somesite.com on mysite.com)

Now I want that if someone arrives at my site with some url strings in it (like utm_source etc) like: mysite.com/?parameter1=value1&parameter2=value2 , I should be able to append some or all parameter values from the mysite link to the somesite link on that site (or iframed on it).

So, say I wanted the somesite link to have to parametr2's value from the mysite link's url, in its another parametr's value. So, lets say I want the somesite link to be : somesite.com/?a1=[Parameter2'sValueHereFromMySiteLink]

I need to be able to fetch any value from any parameter from the source link and append that value anywhere on the somesite link.

I searched the internet and found this solution (but it didn't work):

$('a.my-class').each(function() {
    var href = $(this).attr('href')
    if(href !== undefined) {
        var url = document.URL;
        int index = url.indexOf("Source=");
        if(index != -1){
            url = url.substr(index + 7) // 7 = length of "Source="
            indexx = url.indexOf("&");
            if(indexx != -1)
                url = url.substr(0, indexx + 1);
            // now url contains the value
            href = href.replace("website", url);
            $(this).attr('href', href);
        }
    }
});

Any help would be appreciated. Thanks a lot.

This code gets every field and value from $_GET and creates a text of if:

$text = '?';
$c = 0;
foreach($_GET as $k => $v){
    $text .= ($c>0) ? "&$k=$v" : "$k=$v";
    $c++;
}
$text = ($text=='?') ? "" : $text;

echo $text;

Using your example mysite.com/?parameter1=value1&parameter2=value2 ,
the variable $text has this value ?parameter1=value1&parameter2=value2
So you can use it like this:
<a href=some_url<?php echo $text; ?>>SOME_TEXT</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM