本文介绍了如何获得“GET” JavaScript中的请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从JavaScript中的请求获取GET变量?
How to get "GET" variables from request in JavaScript?
是jQuery还是YUI!有内置的这个功能吗?
Does jQuery or YUI! have this feature built-in?
推荐答案
所有数据都在
window.location.search
你必须解析string,例如。
you have to parse the string, eg.
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
只需用GET变量名作为参数调用函数,例如。
just call the function with GET variable name as parameter, eg.
get('foo');
如果变量没有值或不存在,此函数将返回变量值或undefined
this function will return the variables value or undefined if variable has no value or doesn't exist
这篇关于如何获得“GET” JavaScript中的请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!