Get URL parameters & values with jQuery
原文: 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的更多相关文章
- [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 ...
- Web Modify The Html Elements According Url Parameters With Jquery
需求说明 根据URL的参数, 来批量的对某些HTML元素做统一的修改. 解决思路 首先, 想办法获得这个URL的参数, 然后遍历对应的HTML元素, 做出对应的修改. 即可. 代码实现 <!DO ...
- How to get URL parameters with Javascript?
function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '( ...
- Get Requests with Json Data && Get Requests with Url Parameters
- jQuery 插件 获取URL参数
jQuery 获取URL参数的插件 jQuery Url Query String 下载地址:http://plugins.jquery.com/getUrlQueryString.js/ var ...
- 添加jQuery方法解析url查询部分
Web前端不同页面间传值可以使用 cookies.localStorage 和 sessionStorage 等本地存储. 但是,今天我们尝试使用 url 查询,假设我们要传递字符串 str 到 mo ...
- 转:VS2008 vs2010中JQUERY智能提醒
第一步: 安装VS 2008 SP1 VS 2008 SP1 在Visual Studio中加了更丰富的JavaScript intellisense支持,对很大部分的JavaScript库加了代码完 ...
- c#、sql、asp.net、js、ajax、jquery大学知识点笔记
<table cellSpacing="0" cellPadding="0" width="609" height="470 ...
- ASP.NET之Jquery入门级别
1.Jquery的简单介绍 1)Jquery由美国人John Resig创建.是继prototype之后又一个优秀的JavaScript框架. 2)JQuery能做什么?JQuery能做的普通的Dom ...
随机推荐
- Git for Windows安装和基本设置
1.下载地址: http://msysgit.github.io/ 2.下载完成后安装,安装路径自己选择,其他的选项参照下图: 其他的一步一步往下即可,最后Finish完成安装: 3.配置github ...
- vijos1082丛林探险
P1082丛林探险 描述 东非大裂谷中有一片神秘的丛林,是全世界探险家的乐园,著名黄皮肤探险家BB一直想去试试.正好我国科学家2005年4月将首次对东非大裂谷进行科考,BB决定随科考队去神秘丛林探险. ...
- weblogic启动报错之WLS_DIAGNOSTICS000000.DAT
查看控制台日志报错信息如下: <-- 下午04时46分42秒 CST> <Notice> <Log Management> <BEA-> <The ...
- Android数据加密解密
最近项目在维护过程中,估计这一周都会没有什么事情做了.于是开始打量自己做完的这个项目,项目在展示方面乏善可陈,然后仔细的想了想,这个项目的亮点无非就在数据加密和解密这一块了.因为是银行的项目,所以对数 ...
- kafka中对一个topic增加replicas
是指手动写扩充replicas的配置文件,然后使用工具进行操作. 参考官网site:http://kafka.apache.org/documentation.html#basic_ops_autom ...
- [Locked] Inorder Successor in BST
Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of ...
- JAVA工程师面试总结
HR面: 你为什么要离职?如果是公司层面的,公司为什么要这么做? 你的理想薪资?你觉得你有什么优势胜任你应聘的工作? 你的未来发展规划? BOSS面: 项目的东西,hibernate调优?使用了什么数 ...
- 简单的闭包运算(Closure)演示程序
/* * 该程序用于计算某个产生式的闭包 * RexfieldVon * 2013年8月9日16:01:38 */ #include <stdio.h> #include <stdl ...
- ssh日志记录
上天查看了服务器安全日志,防火墙屏蔽了处理了一些暴力破解ssh密码的ip(其中一个ip地址为北京一家有名的CDN服务提供商),然后删除了所有的/var/log/secure* 日志文件.今天再来查看日 ...
- [转载]Python兵器谱
转载自:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然 ...