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 parametervar 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 parametersvar allVars = $.getUrlVars();

// Getting URL var by its namvar byName = $.getUrlVar(‘name‘);
时间: 2024-08-07 17:21:30

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

Get URL Parameters Total Three Method

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" />     <script src="JS/jquery-1.9.0.js

How to get URL parameters with Javascript?

function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null } So you can use: myvar = getURLParameter('myvar');

jquery的url参数parse和build函数

Parse: function getParam() { var param = new Object() var item = new Array(); if (location.search == "") { return param; }; var query = location.search.substring(1); var list = query.split('&'); for(var i = 0; i < list.length; i++) { item

jQuery 插件 获取URL参数

jQuery 获取URL参数的插件 jQuery Url Query String 下载地址:http://plugins.jquery.com/getUrlQueryString.js/ var url="www.abc.com/index.html?key=hello"; var $arrUrl = $.getUrlQueryString(url); var key=$arrUrl["key"];

jQuery源码

/*! * jQuery JavaScript Library v1.8.3 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2012 jQuery Foundation and other contributors * Released under the MIT license * http://jquery.org/license * * Date: Tue Nov 13 20

jquery 2.1.0 源码

/*! * jQuery JavaScript Library v2.1.0 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ * * Copyright 2005, 2014 jQuery Foundation, Inc. and other contributors * Released under the MIT license * http://jquery.org/license * * Date: 2

jQuery源码解析

( function( global, factory ) { "use strict"; if ( typeof module === "object" && typeof module.exports === "object" ) { module.exports = global.document ? factory( global, true ) : function( w ) { if ( !w.document ) {

《JQuery实战》第4-9章

第四章 事件 命令 定义 参数 返回 bind(eventType,data,listener) 在包装集的所有元素上建立函数,作为指定事件类型的事件处理程序 eventType是事件类型的名称:data(可选)是调用者提供的数据:listener是函数 包装集 one(eventType,data,listener) 在包装集的所有元素上建立函数,作为指定事件类型的事件处理程序.一旦执行,事件处理程序就被自动删除 eventType是事件类型的名称:data(可选)是调用者提供的数据:list

JQuery中的Ajax(六)

一:Ajax请求jQuery.ajax(options) load(url,[data],[callback])jQuery.get(url,[data],[callback]) jQuery.getJSON(url,[data],[callback]) jQuery.getScript(url,[callback]) jQuery.post(url,[data],[callback]) 1.jQuery.ajax(options)通过 HTTP 请求加载远程数据.jQuery 底层 AJAX