jquery get querystring from URL
Possible Duplicate:
How can I get query string values?
I have the following URL:
http://www.mysite.co.uk/?location=mylocation1
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);
Does anyone know how to grab that value using JavaScript or jQuery?
From: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
This is what you need :)
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
This code will return:
{
"me" : "myValue",
"name2" : "SomeOtherValue"
}
and you can do:
var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];
location.search
that is all you need
https://developer.mozilla.org/en-US/docs/DOM/window.location
用一些jQuery和直线JS来做到这一点的简单方法,只需在Chrome或Firefox中查看您的控制台即可查看输出结果...
var queries = {};
$.each(document.location.search.substr(1).split('&'),function(c,q){
var i = q.split('=');
queries[i[0].toString()] = i[1].toString();
});
console.log(queries);
链接地址: http://www.djcxy.com/p/17596.html
下一篇: jquery从URL获取查询字符串