如何在javascript中获取GET变量的值?

时间:2012-08-21 06:46:48

标签: javascript get

  

可能重复:
  Get query string values in JavaScript

如何在javascript中获取get变量?

我想在jquery函数中传递它。

function updateTabs(){

            //var number=$_GET['number']; how can i do this?
        alert(number);
        $( "#tabs" ).tabs({ selected: number });

    }

3 个答案:

答案 0 :(得分:46)

var $_GET = {};
if(document.location.toString().indexOf('?') !== -1) {
    var query = document.location
                   .toString()
                   // get the query string
                   .replace(/^.*?\?/, '')
                   // and remove any existing hash string (thanks, @vrijdenker)
                   .replace(/#.*$/, '')
                   .split('&');

    for(var i=0, l=query.length; i<l; i++) {
       var aux = decodeURIComponent(query[i]).split('=');
       $_GET[aux[0]] = aux[1];
    }
}
//get the 'index' query parameter
alert($_GET['index']);

答案 1 :(得分:3)

写:

var $_GET=[];
window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(a,name,value){$_GET[name]=value;});

然后$_GET['number']

答案 2 :(得分:-3)

试试这个

if (location.search != "")
{
    var x = location.search.substr(1).split(";")
    for (var i=0; i<x.length; i++)
    {
         var y = x[i].split("=");
         alert("Key '" + y[0] + "' has the content '" + y[1]+"'")
    }
}