原文: 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. 【HDOJ】5128

    暴力+计算几何. /* 5128 */ #include <iostream> #include <algorithm> #include <cstdio> #in ...

  2. War3Tool dota改键v3.3版

    wartool魔兽全屏改键功能:1.支持11平台自定义改建,自动进局域网(同类软件暂时没发现这个功能)2.技能改键,可以有效的切换适合你的技能键3.war3路径扫描,运行本程序一键就能打开war3 ( ...

  3. POJ3126 Prime Path(BFS)

    题目链接. AC代码如下: #include <iostream> #include <cstdio> #include <cstring> #include &l ...

  4. 数据结构(Splay平衡树): [NOI2007] 项链工厂

    [NOI2007] 项链工厂 ★★★   输入文件:necklace.in   输出文件:necklace.out   简单对比 时间限制:4 s   内存限制:512 MB [问题描述] T公司是一 ...

  5. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  6. 软件设计模式 B卷

            软件设计模式 试 卷(作业考核 线上)  B  卷   学习中心:            院校学号:             姓名                (共        页 ...

  7. 另一个有趣的Captcha 网站

    今天在一个网站注册时又发现了一个有趣的Captcha形式.给你一个翻转的图片,然后让你拽下面的slide bar让它回到正常的位置,很有趣.下面是提供这个Captcha的网站. minteye – s ...

  8. geektool--一款很geek的工具

    2016/12/18 今天尝试一款很geek的工具 geektool 听名字就超级geek有木有 get it geektool website 从官网直接下载app,一键傻瓜式安装. use it ...

  9. 如何下载coursera视频

    国内观看Coursera非常卡顿,经常播放到一半就卡死了,不知道什么原因.因此只能想办法下载下来之后再看. Github上有一个脚本点击打开链接,提供整门课程的下载服务.用着还是非常方便的,使用方法如 ...

  10. Appium服务器端从启动到case完成的活动分析

    此文的目的主要是通过分析Appium Server打印出来的log,加深对Appium Server所扮演角色的理解. 这整一个过程是由一个Test Case开始执行到结束,测试的对象是SDK自带的N ...