检查是否使用JQuery设置了URL变量

问题描述:

我想知道是否有一个jQuery函数可以检查URL中是否设置了变量.

I would like to know whether there is a jQuery function which can check whether a variable in the URL is set.

类似于PHP中的isset()函数

Something similar to the isset() function in PHP

谢谢

jQuery没有本机函数来获取URL参数.

jQuery doesn't have native functions to get URL parameters.

但是您可以为其编写自己的插件:

But you can write your own plugin to it:

$.extend({
  getUrlVars: function(){
    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;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

然后您可以执行类似的操作:

Then you can do anything like it:

if ($.getUrlVar("MyParam") != null) {
    // Do anything...
}