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 的编码.但一想,就某一个页面的需求而导致其他跟着妥协,不是好的解决方案.于是网上搜索答案,下面做个小分享,遗憾的是研究不够深入, ...
随机推荐
- 利用tesseract-ocr进行验证码识别
因为爬虫项目需要模拟登陆,可是有一个网站的登录需要输入验证码.其实这种登录有2种解决方案,一种是利用cookie,一种是识别图片.前者需要人工登录一次,而且有时效限制,故不太现实.后者可以,但是难点是 ...
- JSP学习(4)
JSP学习(4) JavaBean简介 符合某种规范的Java类.使用JavaBean的好处是解决了代码重复编写,减少代码冗余,功能区分明确,提高代码的可维护性 JavaBean设计原则 JSP动作元 ...
- 随笔4 Dictionary<K,V>
本来说是想介绍一下Hashtable的,但是发现HashMap和Hashtable最开始的不同就是在于HashMap继承了AbstractMap,而Hashtable继承了Dictionary< ...
- jmeter post请求在终端传参,并且指定请求参数文件
命令:jmeter -n -t jmeter_1.jmx -l report/report30.jtl -Jnum=3000 -e -o webresult/3000result ...
- WiFi密码新攻击破解方法,黑客攻破只需10秒
近日,中国知名黑客安全组织东方联盟研究人员透露了一种新的WiFi黑客技术,使黑客更容易破解大多数现代路由器的WiFi密码,并且攻破只需要10秒,速度非常快. 方法是利用由流行的密码破解工具Hashca ...
- Django中如何将javascript中的变量传给位于javascript内的{% url %}中的参数?
这个问题困扰了我一天,不知道困扰了你多久,希望能帮助你 django 中的url模版使用起来可以说是非常方便的,但是怎么在url模版中传入参数的 {% url "url.index" ...
- 026:if标签使用详解
if标签使用详解: if 标签: if 标签相当于 Python 中的 if 语句,有 elif 和 else 相对应,但是所有的标签都需要用标签符号 {% %} 进行包裹. if 标签中可以使 ...
- 网页用html还是php
首先,不管是html还是php,虽然这是两种不同的语言,但是都可以编写网页.不同的是,使用html编写网页是纯静态的网页,无法是运行在本地的,且无法和用户进行交互:而使用php编写的网页则是动态的,运 ...
- 170817关于JSON知识点
1. JSON [1] JSON简介 JSON全称 JavaScript Object Notation ...
- HTML中<input>和<textarea>的区别
在HTML中有两种方式表达文本框 一个是<input>元素的单行文本框 一种是<textarea>的多行文本框. <input>元素: 1.一定要指定type的值为 ...