var test = "http://www.example.org/search?q=whatever&another=moretext";
如何在上面的查询字符串中提取another
's value ( moretext
) 并从中创建一个变量?
var test = "http://www.example.org/search?q=whatever&another=moretext";
如何在上面的查询字符串中提取another
's value ( moretext
) 并从中创建一个变量?
var test = "http://www.example.org/search?q=whatever&another=moretext";
var another = test.split('another=');
another 是一个数组,其中 another[0] = 'http://www.example.org/search?q=whatever&' 和 another[1] = 'moretext'。
将此功能放在您的包中:
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;
}
Usages
alert(querySt("another")+' '+querySt("q"));