原文: http://jquery-howto.blogspot.jp/2009/09/get-url-parameters-values-with-jquery.html

In this post, I would like to share a little jQuery code snippet that makes getting URL parameters and their values more convenient.

Recently, while working on one of my projects, I needed to read and get parameter values from URL string of the current page that was constructed and sent by PHP script. I came across this short and sweet JavaScript code snippet by Roshambo that does just that.

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

The function returns an array/object with your URL parameters and their values. For example, consider we have the following URL:

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

Calling getUrlVars() function would return you the following array:

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

To get a value of first parameter you would do this:

var first = getUrlVars()["me"];

// To get the second parameter
var second = getUrlVars()["name2"];

To make the script syntax to look more jQuery like syntax I rewrote it as an extension for jQuery:

$.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];
  }
});

Now, if you include the above code in your javascript file, you can get URL parameter values in the following way:

// Get object of URL parameters
var allVars = $.getUrlVars(); // Getting URL var by its nam
var byName = $.getUrlVar('name');

Get URL parameters & values with jQuery的更多相关文章

  1. [React Router v4] Use URL Parameters

    URLs can be looked at as the gateway to our data, and carry a lot of information that we want to use ...

  2. Web Modify The Html Elements According Url Parameters With Jquery

    需求说明 根据URL的参数, 来批量的对某些HTML元素做统一的修改. 解决思路 首先, 想办法获得这个URL的参数, 然后遍历对应的HTML元素, 做出对应的修改. 即可. 代码实现 <!DO ...

  3. How to get URL parameters with Javascript?

    function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '( ...

  4. Get Requests with Json Data && Get Requests with Url Parameters

  5. jQuery 插件 获取URL参数

    jQuery 获取URL参数的插件 jQuery Url Query String 下载地址:http://plugins.jquery.com/getUrlQueryString.js/   var ...

  6. 添加jQuery方法解析url查询部分

    Web前端不同页面间传值可以使用 cookies.localStorage 和 sessionStorage 等本地存储. 但是,今天我们尝试使用 url 查询,假设我们要传递字符串 str 到 mo ...

  7. 转:VS2008 vs2010中JQUERY智能提醒

    第一步: 安装VS 2008 SP1 VS 2008 SP1 在Visual Studio中加了更丰富的JavaScript intellisense支持,对很大部分的JavaScript库加了代码完 ...

  8. c#、sql、asp.net、js、ajax、jquery大学知识点笔记

    <table cellSpacing="0" cellPadding="0" width="609" height="470 ...

  9. ASP.NET之Jquery入门级别

    1.Jquery的简单介绍 1)Jquery由美国人John Resig创建.是继prototype之后又一个优秀的JavaScript框架. 2)JQuery能做什么?JQuery能做的普通的Dom ...

随机推荐

  1. POJ Countries in War 3114

    题目大意: 给定一些城市,然后再给一些寄信的路信,A,B,H代表把信从A城市寄到B城市需要H小时. 如果没有直接可以寄达的,可以先通过另外一个城市到达,比如A,B可以寄信,B,C可以寄信,那么,A,C ...

  2. Evaluate Reverse Polish Notation——LeetCode

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  3. 高效算法——B 抄书 copying books,uva714

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description ...

  4. UNIX环境下的消息队列

    消息队列和共享内存一样,也是一种IPC对象.消息队列其实就是消息的链表,每一则消息都是用户自己的结构体.服务端这边创建消息队列,客户端这边打开消息队列,两个进程就可以进行通信.创建和打开消息队列使用函 ...

  5. Windows下移动硬盘无法识别但是Mac下可以识别

    今天遇到一个问题,具体如下: 在Mac下正常使用的移动硬盘,在Windows下无法识别,打开显示"磁盘结构损坏且无法读取" 分析:Mac下既然能够正常使用,那么硬盘就应该是正常的, ...

  6. 《A First Course in Probability》-chaper4-离散型随机变量-负二项分布

    基于我们最为熟悉的离散型分布——二项分布,我们能够衍生出很多别的分布列,对于之前介绍过的几何分布,我们赋予其的含义是:某个事件成功的概率是p,在n次独立重复实验中恰好成功一次的概率是多少.顺着这层含义 ...

  7. Power Calculus 快速幂计算 (IDA*/打表)

    原题:1374 - Power Calculus 题意: 求最少用几次乘法或除法,可以从x得到x^n.(每次只能从已经得到的数字里选择两个进行操作) 举例: x^31可以通过最少6次操作得到(5次乘, ...

  8. kickstrt脚本for cobbler基于system-config-kickstart配置

    1,由于是基于图形界面来生成ks自动安装脚本,所有图形生成ks脚本的服务器首先需要的就是有X Window System; 要是虚机的可以配个tigervnc-servere来进行操作 不说了直接上命 ...

  9. pygame学习资料

    pygame下载地址: https://bitbucket.org/pygame/pygame/downloads 12岁的少年教你用Python做小游戏 Beginning Game Program ...

  10. VB.net 连接池

    上篇博客介绍了临时表的使用,以及它的生命周期和连接池的关系.那么为了能更好的把握临时表的产生和消亡,本篇博客就介绍Vb.net连接池.在打开和关闭数据库连接时的耗费的资源是非常高的.那么在程序需要频繁 ...