检查是否使用JQuery设置了URL变量 [英] Check whether a URL variable is set using JQuery

查看:133
本文介绍了检查是否使用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...
}

这篇关于检查是否使用JQuery设置了URL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆