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

标签 javascript jquery query-string

我想根据查询字符串将自定义 ID 附加到链接,我找到了 this very helpful post作者: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>

最佳答案

这就是你想要的吗:

<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>

关于javascript - 我想根据查询字符串将自定义 ID 附加到链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7790564/

相关文章:

php - Ajax 自动更新和服务器负载

javascript - 返回上一页并输入内容

javascript - 在多个 GridView /表上使用相同的函数

java - Jersey 中单个值的多个 @QueryParam 键

http - 为什么查询字符串在 GET 请求的 URL 中发送并在 POST 请求的正文中发送?

javascript - 放大图像的一部分?

javascript - 将部分 JSON 字符串输出转换为 bool 值

javascript - 是否可以根据Reactjs中实际布局的html来操作组件?

javascript - jQuery 拖放上传...在哪里可以找到?

jquery - 为什么对 getQuery 的调用不起作用?解决办法是什么?