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 的编码.但一想,就某一个页面的需求而导致其他跟着妥协,不是好的解决方案.于是网上搜索答案,下面做个小分享,遗憾的是研究不够深入, ...
随机推荐
- wxpython模板程序,包括各个实例
#coding=utf-8 import wx import time import os class MyApp(wx.App): def __init__(self): wx.App.__init ...
- Latex--入门系列三
Latex 专业的参考 tex对于论文写作或者其他的一些需要排版的写作来说,还是非常有意义的.我在网上看到这个对于Latex的入门介绍还是比较全面的,Arbitrary reference .所以将会 ...
- Robot Framework 源码阅读 day2 TestSuitBuilder
接上一篇 day1 run.py 发现build test suit还挺复杂的, 先从官网API找到了一些资料,可以看出这是robotframework进行组织 测试案例实现的重要步骤, 将传入的te ...
- 【vue】父子组件间通信----传函数
(一)子组件 调用 父组件 方法 方式一) 子组件中通过this.$parent.event来调用父组件的方法 父组件 <template> <div> <child&g ...
- 实现 unity MonoBehaviour API5.4 的消息
顺序(第一次执行.忽略循环) 方法 说明 Editor 1 void Reset() 重置为默认值 ------------------------------------------------ ...
- Apache Mesos 官方文档 V1.0
Apache Mesos 官方文档 V1.0 2016-11-07 中文版:http://mesos.mydoc.io/ gitBook :https://www.gitbook.com/book/m ...
- Maya2017下载安装与激活
目录 1. 更多推荐 2. 下载地址 2.1. OneDrive 2.2. 百度云 3. 安装激活步骤 1. 更多推荐 其他Maya版本的下载与激活:https://www.cnblogs.com/c ...
- no hash tools
import itertools class Set(list): def __init__(self, params): super(Set, self).__init__() ...
- 前端每日实战:32# 视频演示如何用纯 CSS 创作六边形按钮特效
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xjoOeM 可交互视频教程 此视频 ...
- [洛谷P2296] NOIP2014 寻找道路
问题描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点连通. 2 .在满足条 ...