Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

javascript - jquery从URL获取查询字符串[重复](jquery get querystring from URL [duplicate])

Possible Duplicate:

(可能重复:)

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:

(我需要的是从URL获取location的值到一个变量,然后在jQuery代码中使用它:)

var thequerystring = "getthequerystringhere"

$('html,body').animate({scrollTop: $("div#" + thequerystring).offset().top}, 500);

Does anyone know how to grab that value using JavaScript or jQuery?

(有谁知道如何使用JavaScript或jQuery获取该值?)

  ask by Tom translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From: http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

(来自: 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:

(以下代码将返回包含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;
}

For example, if you have the URL:

(例如,如果您有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"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...