我想根据查询字符串将自定义 ID 附加到链接

发布于 2024-12-10 13:30:22 字数 1916 浏览 0 评论 0原文

我想根据查询字符串将自定义 ID 附加到链接,我发现 这篇非常有用的文章,作者:Gregory(请查看它以了解一些背景信息)。

我想要实现的是,如果我访问 www.mydomain.com,我需要将默认值添加到网页上的链接,例如:www.ramdomdomain.com ?myID1=default_value

我猜想必须在代码中的 if(hrefvalue==null) 行之后编辑一些内容,但我不知道该怎么做。请帮我。以下是我正在使用的代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script type="text/javascript">
function getHrefValue(key,url){
  var query=new RegExp("[\\?&]"+(key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"))+"=([^&#]*)").exec(url);
  return (query==null)?null:query[1];
}
function matchHrefs(){
  var config={
    'myid1':["#topnav a[href^='http://www.domain.com']"],
    'myid2':["#topnav a[href^='http://www.domain.com']"]
  }
  for(var current in config){
    var myvalue=getHrefValue(current,location.search);
    if(myvalue!=null&&myvalue!=""){
      $(config[current].join(',')).each(function(){
        var href=$(this).attr('href');
        var hrefvalue=getHrefValue(current,href);
        if(hrefvalue==null){
          var href_split=href.split('#');
          $(this).attr('href',href_split[0]+(href_split[0].indexOf('?')>-1?'&':'?')+current+'='+myvalue+(typeof(href_split[1])!="undefined"?'#'+href_split[1]:''));
          $(this).addClass('selected selected-'+current);
        }
        if(hrefvalue==""){
          $(this).attr('href',href.replace(current+'=',current+'='+myvalue));
          $(this).addClass('selected selected-'+current);
        }
      });
    }
  }
}
$(function(){matchHrefs();});
</script>

<div id="topnav"><a href="http://www.domain.com/" target="_blank">Random domain</a></div>

I want to attach custom ids to links based on query string and I found this very helpful post by Gregory (please take a look at it to get some context).

What I am trying to achieve is, if I go to www.mydomain.com, i require a default value to be added to the link on the webpage, example: www.ramdomdomain.com?myID1=default_value

I guess something must be edited after the if(hrefvalue==null) line in the code, but I can't figure out how to do it. Please help me. The following is the code I'm using:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script type="text/javascript">
function getHrefValue(key,url){
  var query=new RegExp("[\\?&]"+(key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"))+"=([^&#]*)").exec(url);
  return (query==null)?null:query[1];
}
function matchHrefs(){
  var config={
    'myid1':["#topnav a[href^='http://www.domain.com']"],
    'myid2':["#topnav a[href^='http://www.domain.com']"]
  }
  for(var current in config){
    var myvalue=getHrefValue(current,location.search);
    if(myvalue!=null&&myvalue!=""){
      $(config[current].join(',')).each(function(){
        var href=$(this).attr('href');
        var hrefvalue=getHrefValue(current,href);
        if(hrefvalue==null){
          var href_split=href.split('#');
          $(this).attr('href',href_split[0]+(href_split[0].indexOf('?')>-1?'&':'?')+current+'='+myvalue+(typeof(href_split[1])!="undefined"?'#'+href_split[1]:''));
          $(this).addClass('selected selected-'+current);
        }
        if(hrefvalue==""){
          $(this).attr('href',href.replace(current+'=',current+'='+myvalue));
          $(this).addClass('selected selected-'+current);
        }
      });
    }
  }
}
$(function(){matchHrefs();});
</script>

<div id="topnav"><a href="http://www.domain.com/" target="_blank">Random domain</a></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-12-17 13:30:22

这就是你所追求的吗:

<div>        
        <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
        <script type="text/javascript">
            $(function() {
                var requestid = new String(gup('myid'));
                if (requestid == "") {
                    requestid = "unknown";
                }
                $("a").each(function() {
                    var href = $(this).attr("href");
                    href += "?myId=" + requestid;
                    $(this).attr("href", href);
                })
            })

            //gup taken from here:http://www.netlobo.com/url_query_string_javascript.html
            function gup(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 results[1];
            }

        </script>

        <div id="topnav">
            <a href="http://www.google.com" target="_blank">Random domain</a>
            <a href="http://www.yahoo.com" target="_blank">Random domain</a>
        </div>
    </div>

Is this what you're after:

<div>        
        <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
        <script type="text/javascript">
            $(function() {
                var requestid = new String(gup('myid'));
                if (requestid == "") {
                    requestid = "unknown";
                }
                $("a").each(function() {
                    var href = $(this).attr("href");
                    href += "?myId=" + requestid;
                    $(this).attr("href", href);
                })
            })

            //gup taken from here:http://www.netlobo.com/url_query_string_javascript.html
            function gup(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 results[1];
            }

        </script>

        <div id="topnav">
            <a href="http://www.google.com" target="_blank">Random domain</a>
            <a href="http://www.yahoo.com" target="_blank">Random domain</a>
        </div>
    </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文