这个javascript代码可以优化吗?

时间:2011-11-14 13:15:37

标签: javascript jquery html css

如果当前页面URL在查询字符串中有一个参数'myid1'或'myid2',对于我的网页中每个具有'rewrite'类的链接,我希望链接href的查询字符串被当前页面URL的查询字符串替换。我正在使用下面给出的代码。由于我是javascript的新手,我不确定它是否已经过优化。我希望它尽快执行。请帮忙。在此先感谢:)

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>

<script type="text/javascript">
$(function() {
    var requestid = gup('myid1');
    if (requestid) {
        $("a.rewrite").each(function() {
            var base = this.href;
            var pos = base.indexOf("?");
            if (pos != -1) {
                base = base.substr(0, pos);
            }
            this.href = base + "?myid1=" + requestid;
        })
    }
    var requestid2 = gup('myid2');
    if (requestid2) {
        $("a.rewrite").each(function() {
            var base = this.href;
            var pos = base.indexOf("?");
            if (pos != -1) {
                base = base.substr(0, pos);
            }
            this.href = base + "?myid2=" + requestid2;
        })
    }
})

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>

<a href="http://www.website.com/?someid=1234" class="rewrite">Hyperlink</a>

2 个答案:

答案 0 :(得分:0)

$(function() {
    var requestid = gup('myid1');
    var requestid2 = gup('myid2');

    if (requestid || requestid2) {
        $("a.rewrite").each(function() {
            var base = this.href;
            var pos = base.indexOf("?");
            if (pos != -1) {
                base = base.substr(0, pos);
            }

            if (requestid){
                this.href = base + "?myid1=" + requestid;
                if (requesid2){
                    this.href += "?myid2=" + requestid;
                }
            } else {
                this.href = base + "?myid2=" + requestid;
            }

        })
    }

});

答案 1 :(得分:0)

您提供的代码在两个方面效率低下:

  1. 不必要的循环。它每次循环a.rewrite锚点以进行一次查询字符串匹配。它可以优化为一个循环;

  2. 正常表达式匹配
  3. 重复计算。每个regexS函数都会执行gup,并且可以将其缩减为一个计算。

  4. 解决方案是:

    1. window.location.href数据提取到一个可以在以后引用的变量中;

    2. 将两个(或多个)循环合二为一,并在一个循环中完成所有替换。

    3. 这里优化的代码是:

      //First you fetch the query string as key-value pairs in the window.location.href, this equals your gup function.
      
      //This code, fetch the ?key1=value1&key2=value2 pair into an javaScript Object {'key1': 'value1', 'key2':'value2'}
      var queryString = {}; 
      var queryStringPattern = new RegExp("([^?=&]+)(=([^&]*))?", "g");
      window.location.href.replace(
          queryStringPattern,
          function($0, $1, $2, $3) { queryString[$1] = $3; }
      );
      
      //Second you collect all the anchor with class rewrite and execute the replacement.
      
      $("a.rewrite").each(function () {
        this.href.replace(
          queryStringPattern,
          function ($0, $1, $2, $3) {
            return queryString[$1] ? $1 +  "=" + queryString[$1] : $1 + '=' + $3;
          }
        )
      });
      
相关问题