将 urlVariable 附加到下一页的链接 [英] Append urlVariable to next page's link

查看:28
本文介绍了将 urlVariable 附加到下一页的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置页面以将 urlVariable 附加到其链接中?

How do i set a page to append a urlVariable into its links?

例如,我有 3 个不同的页面,并且所有页面都链接到同一页面,例如
接收器.html

for example, i have 3 different pages and all are linked into the same page, e.g.
receiver.html

首页链接:

receiver.html?sender=1

第二页链接:

receiver.html?sender=2

第三页链接:

receiver.html?sender=3

当第一页被点击时,它会将用户发送到receiver.html,里面有很多传出链接,脚本会根据上面的三个页面将变量附加到它的所有传出链接中?

when the first page is clicked it will send the user to receiver.html which has many outgoinglinks inside, and the script will append the variable into all its outgoing links depending on the three pages above?

接收器.html

outgoinglink.html?sender=1
outgoinglink2.html?sender=1
outgoinglink3.html?sender=1

如果使用第二页,receiver.html 将附加

and if second page is used, the receiver.html will append

?sender=2

关于它内部的所有链接等等..

on all its link inside and so on and so forth..

推荐答案

你可以使用这篇文章中显示的函数 (http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) 获取您的参数

Well you could use the function shown in this post (http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript) to get your parameter

function getParameterByName( name )

    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return "";
      else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
    }

var sender = getParameterByName('sender');

然后将您需要的内容附加到所有 href 中:

then attach what you need to all href:

$('a').each(function(){
    var href = $(this).attr('href');
    if (href.indexOf('&') !== -1){
        //if you have on & in the href you must use &
        $(this).attr('href', href+'&sender='+sender);
     }else{
        $(this).attr('href', href+'?sender='+sender);
     }
});

这篇关于将 urlVariable 附加到下一页的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆