Javascript 获取 URL 参数

1
59
function getArgs( ) {
     var args = new Object( );
     var query = location.search.substring(1);      // Get query string
     var pairs = query.split("&");                  // Break at ampersand
     for(var i = 0; i < pairs.length; i++) {
         var pos = pairs[i].indexOf('=');           // Look for "name=value"
         if (pos == -1) continue;                   // If not found, skip
         var argname = pairs[i].substring(0,pos); // Extract the name
         var value = pairs[i].substring(pos+1);     // Extract the value
         value = decodeURIComponent(value);         // Decode it, if needed
         args[argname] = value;                     // Store as a property
     }
     return args;                                   // Return the object
}

返回是个数组,
alert(getArgs()['str']);或alert(getArgs().str);
'str' 是url 中参数变量名。

1 COMMENT

  1. facebook find love –
    I constantly spent my half an hour to read this
    blog’s articles or reviews everyday along with a cup of coffee.

LEAVE A REPLY

Please enter your comment!
Please enter your name here