Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中获取“GET”请求参数?(How to get “GET” request parameters in JavaScript?)

How to get "GET" variables from request in JavaScript?

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

Does jQuery or YUI!

(是jQuery还是YUI!)

have this feature built-in?

(有内置的这个功能吗?)

  ask by Daniel Silveira translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All data is available under

(所有数据均可在)

window.location.search

you have to parse the string, eg.

(你必须解析字符串,例如。)

function get(name){
   if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
      return decodeURIComponent(name[1]);
}

just call the function with GET variable name as parameter, eg.

(只需使用GET变量名称作为参数调用该函数,例如。)

get('foo');

this function will return the variables value or undefined if variable has no value or doesn't exist

(如果变量没有值或不存在,此函数将返回变量值或undefined)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...