假设我有一个类似“www.example.com/example.html?storeid=1400&shopperid=1200”的网址,我想提取shopperid等于的信息,所以在这个例子中它将是1200.I将此脚本添加到添加到html的标头中。为什么这不起作用?
JS:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var shopperid = getParameterByName('shopperid');
答案 0 :(得分:1)
您的网址格式不正确。你有两个?,第二个应该是&。
"?www.example.com/example.html STOREID = 1400 shopperid = 1200"
为:
" www.example.com/example.html STOREID = 1400安培; shopperid = 1200"