js获取url传递参数,js获取url?号后面的参数
js获取url传递参数,js获取url?号后面的参数
方法一、正则表达式
1 2 3 4 5 6 | function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } |
方法二、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < Script language="javascript"> function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length ; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; } </script> |
方法三、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * 获取指定的URL参数值 * URL:http://www.quwan.com/index?name=tyler * 参数:paramName URL参数 * 调用方法:getParam("name") * 返回值:tyler */ function getParam(paramName) { paramValue = "", isFound = !1; if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0; while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++ } return paramValue == "" && (paramValue = null), paramValue } |
其他参数获取介绍:
//设置或获取对象指定的文件名或路径。
alert(window.location.pathname);
//设置或获取整个 URL 为字符串。
alert(window.location.href);
//设置或获取与 URL 关联的端口号码。
alert(window.location.port);
//设置或获取 URL 的协议部分。
alert(window.location.protocol);
//设置或获取 href 属性中在井号“#”后面的分段。
alert(window.location.hash);
//设置或获取 location 或 URL 的 hostname 和 port 号码。
alert(window.location.host);
//设置或获取 href 属性中跟在问号后面的部分。
alert(window.location.search);
</div>
<div class="postDesc">posted @ <span id="post-date">2016-10-23 22:47</span> <a href="http://www.cnblogs.com/karila/">karila</a> 阅读(<span id="post_view_count">28028</span>) 评论(<span id="post_comment_count">2</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=5991340" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(5991340);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=270671,cb_entryId=5991340,cb_blogApp=currentBlogApp,cb_blogUserGuid='0885a2a9-b2e2-e511-9fc1-ac853d9f53cc',cb_entryCreatedDate='2016/10/23 22:47:00';loadViewCount(cb_entryId);var cb_postType=1;</script>
评论列表
<div class="feedbackItem">
<div class="feedbackListSubtitle">
<div class="feedbackManage">
<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(3750762,'R6YHyZHBM3BITuqDEoG2QnV/B6BGVrgWu3IqxJLVTG6ob1P3GvQZlw==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(3750762,'R6YHyZHBM3BITuqDEoG2QnV/B6BGVrgWu3IqxJLVTG6ob1P3GvQZlw==')">引用</a></span>
</div>
<a href="#3750762" class="layer">#1楼</a><a name="3750762" id="comment_anchor_3750762"></a> <span class="comment_date">2017-08-04 16:42</span> <a id="a_comment_author_3750762" href="http://www.cnblogs.com/paxster/" target="_blank">Paxster</a> <a href="http://msg.cnblogs.com/send/Paxster" title="发送站内短消息" class="sendMsg2This"> </a>
</div>
<div class="feedbackCon">
<div id="comment_body_3750762" class="blog_comment_body">第一种方法有问题</div><div class="comment_vote"><a href="javascript:void(0);" class="comment_digg" onclick="return voteComment(3750762,'Digg',this)">支持(0)</a><a href="javascript:void(0);" class="comment_bury" onclick="return voteComment(3750762,'Bury',this)">反对(0)</a></div><span id="comment_3750762_avatar" style="display:none;">http://pic.cnblogs.com/face/522856/20130429100908.png</span>
</div>
</div>
<div class="feedbackItem">
<div class="feedbackListSubtitle">
<div class="feedbackManage">
<span class="comment_actions"><a href="javascript:void(0);" onclick="return ReplyComment(3750764,'wCZZjCjoYUoYA12GZ9K3QVnzCQrEVg/jRW+50h4IDCn584OawobqHA==')">回复</a><a href="javascript:void(0);" onclick="return QuoteComment(3750764,'wCZZjCjoYUoYA12GZ9K3QVnzCQrEVg/jRW+50h4IDCn584OawobqHA==')">引用</a></span>
</div>
<a href="#3750764" class="layer">#2楼</a><a name="3750764" id="comment_anchor_3750764"></a>[<span class="louzhu">楼主</span>]<span id="comment-maxId" style="display:none;">3750764</span><span id="comment-maxDate" style="display:none;">2017/8/4 16:44:53</span> <span class="comment_date">2017-08-04 16:44</span> <a id="a_comment_author_3750764" href="http://www.cnblogs.com/karila/" target="_blank">karila</a> <a href="http://msg.cnblogs.com/send/karila" title="发送站内短消息" class="sendMsg2This"> </a>
</div>
<div class="feedbackCon">
<div id="comment_body_3750764" class="blog_comment_body"><a href="#3750762" title="查看所回复的评论" onclick="commentManager.renderComments(0,50,3750762);">@</a>
PHSeven
有什么问题呢
支持(0)反对(0)
</div>