猿问

如何在JavaScript中获取“GET”请求参数?

如何在JavaScript中获取“GET”请求参数?

如何从JavaScript中的请求中获取“GET”变量?

是jQuery还是YUI!有内置的这个功能吗?


料青山看我应如是
浏览 974回答 3
3回答

蛊毒传说

所有数据均可在window.location.search你必须解析字符串,例如。function get(name){    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))       return decodeURIComponent(name[1]);}只需使用GET变量名称作为参数调用该函数,例如。get('foo');如果变量没有值或不存在,此函数将返回变量值或undefined

炎炎设计

如果您想要一个包含所有请求的对象,只需将我的两分钱function&nbsp;getRequests()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;s1&nbsp;=&nbsp;location.search.substring(1,&nbsp;location.search.length).split('&'), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;{},&nbsp;s2,&nbsp;i; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;s1.length;&nbsp;i&nbsp;+=&nbsp;1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s2&nbsp;=&nbsp;s1[i].split('='); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r[decodeURIComponent(s2[0]).toLowerCase()]&nbsp;=&nbsp;decodeURIComponent(s2[1]); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r;};var&nbsp;QueryString&nbsp;=&nbsp;getRequests();//if&nbsp;url&nbsp;===&nbsp;"index.html?test1=t1&test2=t2&test3=t3"console.log(QueryString["test1"]);&nbsp;//logs&nbsp;t1console.log(QueryString["test2"]);&nbsp;//logs&nbsp;t2console.log(QueryString["test3"]);&nbsp;//logs&nbsp;t3注意,每个get参数的键设置为小写。所以,我做了一个帮手功能。所以现在它不区分大小写。function&nbsp;Request(name){ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;QueryString[name.toLowerCase()];}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答