我有以下网址:
http://www.mysite.co.uk/?location=mylocation1
我需要location
将URL 的值转换成变量,然后在jQuery代码中使用它:
var thequerystring = "getthequerystringhere"
$('html,body').animate({scrollTop: $("div#" + thequerystring).offset().top}, 500);
如何使用JavaScript或jQuery来获取该值?
来自:http : //jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
这就是你所需要的:)
以下代码将返回一个包含URL参数的JavaScript对象:
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
例如,如果您具有URL:
http://www.example.com/?me=myValue&name2=SomeOtherValue
此代码将返回:
{
"me" : "myValue",
"name2" : "SomeOtherValue"
}
您可以执行以下操作:
var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句