jquery从URL获取查询字符串 [英] jquery get querystring from URL
本文介绍了jquery从URL获取查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何获取查询字符串值?
我有以下网址:
http://www.mysite.co.uk/?location=mylocation1
我需要的是获得位置的值
从URL变为变量,然后在jQuery代码中使用它:
What I need is to get the value of location
from the URL into a variable and then use it in a jQuery code:
var thequerystring = "getthequerystringhere"
$('html,body').animate({scrollTop: $("div#" + thequerystring).offset().top}, 500);
有没有人知道如何使用JavaScript或jQuery获取该值?
Does anyone know how to grab that value using JavaScript or jQuery?
推荐答案
来自: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
这就是你所需要的:)
以下代码将返回一个包含URL参数的JavaScript对象:
The following code will return a JavaScript Object containing the URL parameters:
// 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;
}
例如,如果您有以下网址:
For example, if you have the URL:
http://www.example.com/?me=myValue&name2=SomeOtherValue
此代码将返回:
{
"me" : "myValue",
"name2" : "SomeOtherValue"
}
你可以这样做:
var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];
这篇关于jquery从URL获取查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文