Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript获取URL参数?_Javascript - Fatal编程技术网

如何使用Javascript获取URL参数?

如何使用Javascript获取URL参数?,javascript,Javascript,可能重复: Javascript没有提供核心方法来实现这一点,那么如何实现呢 function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null; } 因此,

可能重复:


Javascript没有提供核心方法来实现这一点,那么如何实现呢

function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
因此,您可以使用:

myvar = getURLParameter('myvar');

使用图书馆谢谢你的回答。添加了一个编辑,以考虑没有值的参数的边缘情况。@Deanmehan如果它“正常工作”,那就太棒了,但考虑到正则表达式的可读性,我不会说这是一个简单的解决方案。如果URI中有一个哈希标记,这是否有效?如果Chrome中有一个
#something
,那么你必须使用
window.location.hash
而不是
window.location.search
…如果a)解释发生了什么并且b)通过了JSHint验证,那么就会进行投票:目前,[,”“]结构无效。如何获取数组参数的值,例如