jquery get querystring from URL [duplicate]

Możliwy duplikat:
Jak mogę uzyskać wartości ciągów zapytań?

Mam następujący URL:

http://www.mysite.co.uk/?location=mylocation1

To, czego potrzebuję, to pobranie wartości location z adresu URL do zmiennej, a następnie użycie jej w kodzie jQuery:

var thequerystring = "getthequerystringhere"

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

Czy ktoś wie jak pobrać tę wartość za pomocą JavaScript lub jQuery?

Author: Community, 2011-01-11

5 answers

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

To jest to czego potrzebujesz:)

Poniższy kod zwróci obiekt JavaScript zawierający parametry URL:

// 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;
}

Na przykład, jeśli masz URL:

http://www.example.com/?me=myValue&name2=SomeOtherValue

Ten kod zwróci:

{
    "me"    : "myValue",
    "name2" : "SomeOtherValue"
}

I możesz zrobić:

var me = getUrlVars()["me"];
var name2 = getUrlVars()["name2"];
 419
Author: benhowdle89,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-07 00:00:29
 146
Author: Yene Mulatu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-30 06:46:39

Prosty sposób na to z jQuery i straight JS, wystarczy wyświetlić konsolę w Chrome lub Firefox, aby zobaczyć wyjście...

  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);
 26
Author: James,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-31 12:59:03

Spójrz na tę odpowiedź stackoverflow .

 function getParameterByName(name, url) {
     if (!url) url = window.location.href;
     name = name.replace(/[\[\]]/g, "\\$&");
     var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
         results = regex.exec(url);
     if (!results) return null;
     if (!results[2]) return '';
     return decodeURIComponent(results[2].replace(/\+/g, " "));
 }

Możesz użyć metody animacji:

Ie:

var thequerystring = getParameterByName("location");
$('html,body').animate({scrollTop: $("div#" + thequerystring).offset().top}, 500);
 13
Author: Steve,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-19 13:57:46

Robimy to w ten sposób...

String.prototype.getValueByKey = function (k) {
    var p = new RegExp('\\b' + k + '\\b', 'gi');
    return this.search(p) != -1 ? decodeURIComponent(this.substr(this.search(p) + k.length + 1).substr(0, this.substr(this.search(p) + k.length + 1).search(/(&|;|$)/))) : "";
};
 1
Author: Adriano Galesso Alves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-26 14:58:07