【问题标题】:Get a variable from url parameter using Javascript使用Javascript从url参数获取变量
【发布时间】:2012-01-17 14:49:19
【问题描述】:

我正在尝试使用 javascript 获取 url 值,到目前为止,我只能获取纯数字,而不是混合数字与字母或仅字母。我找不到任何允许检索带有字母的数字的函数的工作示例,只是数字。我没有使用任何非字母数字字符。我试图传递的一个示例值是“42p316041610751874cm83p2306600132141”。

function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
}
return vars;
}

 var first = getUrlVars()["test"];

任何帮助都会很棒。谢谢。

【问题讨论】:

  • 这很奇怪。您的代码应该可以工作。
  • 我试过了,它对我有用。我使用了这个网址:http://localhost/test/index.html?test=42p316041610751874cm83p2306600132141
  • 如果这很重要,我正在使用 jQuery Mobile 作为网站的框架?

标签: javascript url


【解决方案1】:

以下是上述两个答案的精简版:

function gup (name) {
  name = RegExp ('[?&]' + name.replace (/([[\]])/, '\\$1') + '=([^&#]*)');
  return (window.location.href.match (name) || ['', ''])[1];
}

更容易打字,更容易处理,恕我直言,更容易阅读。 YMMV

【讨论】:

    【解决方案2】:

    我使用了这个功能,它很震撼。我不记得从哪里拿的了,但它是个好东西:

    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];
    } 
    

    如果你有http://www.exmaple.com/path?p1=lkjsd234&amp;p2=klsjd987这样的网址,你可以使用:

    alert(gup('p1')); // shows 'lkjsd234';
    

    【讨论】:

    【解决方案3】:

    您的代码看起来应该可以工作,如果有帮助,我使用下面的函数

     function getParam(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];  
    }
    
    var first = getParam("test");
    

    【讨论】:

      【解决方案4】:

      您的方法应该有效。这是我的比较版本:

      var o = {
          keys: [ ],
         values: [ ]
      }
      
      /* 
      You could just use the window#location#search value to get the query sub-String of the currently loaded URL
      
       var q = window.location.search.substring(1) ; 
      
      For now we'll use a query String from a Google search for "MDN window location"
      */
      
      q = "q=mdn+window+location&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:de:official&client=firefox-a"
      
      for( var i = 0 , a = q.split("&"), p ; i < a.length ; i++ ) {
          p = a[i] ;
          if(  ( b = p.split("=") )  !=  null ) {
              o.keys[i] = b[0] ;
              o.values[i] = b[1] ;
          }
      }
      
      
      console.log("(!!) o: " + o.toSource( ) ) ;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-07
        • 2011-05-26
        • 2019-05-21
        • 1970-01-01
        • 2015-01-18
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        相关资源
        最近更新 更多