var test = "http://www.example.org/search?q=whatever&another=moretext";
如何在上面的查询字符串中提取another
的值(moretext
)并从中创建变量?
答案 0 :(得分:0)
var test = "http://www.example.org/search?q=whatever&another=moretext";
var another = test.split('another=');
另一个是另一个数组[0] ='http://www.example.org/search?q=whatever&'另一个[1] ='moretext'。
答案 1 :(得分:0)
将此功能保存在您的包中:
function querySt(qsName, url)
{
var theUrl;
if (url == null || url == undefined)
theUrl = window.location.search.substring(1); else theUrl = url;
var g = theUrl.split("&");
for (var i = 0; i < g.length; i++) {
var pair = g[i].split("=");
if (pair[0].toLowerCase() == qsName.toLowerCase())
{
return pair[1];
}
}
return null;
}
用法
alert(querySt("another")+' '+querySt("q"));