Reading query string values in JavaScript
Most server software can read values from the query string easily, but sometimes you need to read these values in the browser using JavaScript.
The window.location object contains some handy information about whatever you may have in your browser’s address bar. For example if you would visithttps://www.example.com/some/resource?foo=bar&q=baz , you will get this object:
JSON.stringify(window.location, null, ); // => {   "hash": "",   "search": "?foo=bar&q=baz",   "pathname": "/some/resource",   "port": "",   "hostname": "www.example.com",   "host": "www.example.com",   "protocol": "https:",   "href": "https://www.example.com/some/resource?foo=bar&q=baz" } 
So the search key contains the query string, however it’s not very usable as it is. We need to parse it. Assuming a non-empty query string you can do this:
var parsedQuery = (function () {   var query = window.location.search,       parsed = {};   // first get rid of the “?”   query = query.substr();   // get the different query string fields by splitting on “&”   query = query.split('&');   // iterate over each field’s key and value and assign value to parsed[key]   for (var i = ; i < query.length; i++) {     // get key and value by splitting on “=”     var field = query[i].split('='),         key = window.decodeURIComponent(field[]),         value = window.decodeURIComponent(field[]);     parsed[key] = value;   }   // return parsed query   return parsed; }()); 
That’s all there is to it. Everything nicely inside an immediately-invoked function expression (IIFE) so we don’t pollute global scope with all those variables.
Keep in mind all values will be strings, so if your application expects otherwise you have to convert them yourself.
To conclude this short article I thought it would be nice to give you a minified version with an applicable isogram as function signature.
var parsedQuery = (function(q,u,e,r,y){for(r=;r<q.length;r++){y=q[r].split('=');u[e(y[])]=e(y[])}return u})(location.search.substr().split('&'),{},decodeURIComponent);												
											Reading query string values in JavaScript的更多相关文章
- How to get the query string by javascript?
		
http://techfunda.com/Tools/XmlToJson http://beautifytools.com/xml-to-json-converter.php https://www. ...
 - Are query string keys case sensitive?
		
Are query string keys case sensitive? @gbjbaanb's answer is incorrect: The RFCs only specify the all ...
 - convert URL Query String to Object All In One
		
convert URL Query String to Object All In One URL / query string / paramas query string to object le ...
 - nodejs笔记三--url处理、Query String;
		
URL--该模块包含用以 URL 解析的实用函数. 使用 require('url') 来调用该模块. 一.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看 ...
 - <原>ASP.NET 学习笔记之HTML helper中参数何时会是路由参数,何时又会是query string?
		
HTML helper中参数何时会是路由参数,何时又会是query string? @Html.ActionLink("Edit", "Edit", new ...
 - query string parameters 、 Form Data 、 Request Payload
		
微信小程序ajax向后台传递参数的时候总是报400错误 然后看了一下network 发现是query string parameters,但是我写的header如下 header:{ "Co ...
 - springMVC接收参数的区别form data与query string parameters与request payload
		
在AJAX请求中,我见过有三种form表单数据类型提交. 第一种:form data, 第二种:query string parameters,第三种:request payload. 在google ...
 - http 请求参数之Query String Parameters、Form Data、Request Payload
		
Query String Parameters 当发起一次GET请求时,参数会以url string的形式进行传递.即?后的字符串则为其请求参数,并以&作为分隔符. 如下http请求报文头: ...
 - asp.net query string 及 form data 遇到的编码问题
		
当遇到此问题时,脑海里闪过的第一个解决方案是设置 web.config 的编码.但一想,就某一个页面的需求而导致其他跟着妥协,不是好的解决方案.于是网上搜索答案,下面做个小分享,遗憾的是研究不够深入, ...
 
随机推荐
- 使用vue-resource请求数据的步骤
			
1.需要安装 vue-resource模块 注意加上--save npm install vue-resource --save 2.main.js 引入vue-resource import Vue ...
 - 分布式锁的实现【基于ZooKeeper】
			
引言 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提 ...
 - gcc的-D,-w,-W,-Wall,-O3这些参数的意义
			
一.-D 其意义是添加宏定义,这个很有用. 当你想要通过宏控制你的程序,不必傻乎乎的在程序里定义,然后需要哪个版本,去修改宏. 只需要在执行gcc的时候,指定-D,后面跟宏的名称即可. 示例: gcc ...
 - robotframework关键字
			
*** Settings ***Library Selenium2Library *** Keywords ***Checkbox应该不被选择 [Arguments] ${locator} Check ...
 - 六、MyBatis-缓存机制
			
MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制.缓存可以极大的提升查询效率.MyBatis系统中默认定义了两级缓存, 一级 缓存和 二级缓存.– 1.默认情况下,只有一级缓 ...
 - BZOJ 5046 分糖果游戏
			
网页崩溃了 心态也崩溃了 MD劳资写了那么多 题意: 有a,b两个人分糖,每个人都有一个能量值.每个人每一轮可以选择进行两种操作: 1.取走最左边的糖果,补充相应的能量值并获取相应的美味度. 2.跳过 ...
 - flask之Twitter Bootstrap
			
一:Twitter Bootstrap是什么? 1.开源框架:提供用户页面组件. 2.可以创建整洁且具有吸引力的网站,并且网站能兼容所有现代的Web浏览器. 特点: Bootstrap 是客户端框架, ...
 - MySQL WAL
			
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11447794.html WAL: Write-Ahead Logging 先写日志,再写磁盘.具体说, ...
 - 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
			
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
 - (arm板子tensorflow安装)armv7板子pip安装的wheel
			
树莓派之类的armv7板子在,安装 numpy,scipy时经常失败,因为安装过程是下载源码包到本地编译,然后再安装的,编译过程中往往就会失败. https://www.piwheels.org/si ...