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

Nir*_*shi 12 javascript get

可能重复:
在JavaScript中获取查询字符串值

如何在javascript中获取get变量?

我想在jquery函数中传递它.

function updateTabs(){

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

    }
Run Code Online (Sandbox Code Playgroud)

gio*_*_13 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']);
Run Code Online (Sandbox Code Playgroud)

  • 最好将decodeURIComponent用于:参数名称和参数值. (2认同)

Mag*_*eek 7

写:

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

然后 $_GET['number']