Javascript 获取 URL 参数

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 中参数变量名。

Related posts:

  1. javascript+css 新闻滑轮显示
  2. URL Redirect 网址重定向的方法
  3. JQuery 新闻头条淡入淡出效果
  4. JavaScript可移动弹出层
  5. jQuery插件Step Carousel Viewer实现Javascript图片滑动旋转效果
此条目发表在 前端技术 分类目录,贴了 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word