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的更多相关文章

  1. How to get the query string by javascript?

    http://techfunda.com/Tools/XmlToJson http://beautifytools.com/xml-to-json-converter.php https://www. ...

  2. Are query string keys case sensitive?

    Are query string keys case sensitive? @gbjbaanb's answer is incorrect: The RFCs only specify the all ...

  3. 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 ...

  4. nodejs笔记三--url处理、Query String;

    URL--该模块包含用以 URL 解析的实用函数. 使用 require('url') 来调用该模块. 一.parse函数的基础用法 parse函数的作用是解析url,返回一个json格式的数组,请看 ...

  5. <原>ASP.NET 学习笔记之HTML helper中参数何时会是路由参数,何时又会是query string?

    HTML helper中参数何时会是路由参数,何时又会是query string?   @Html.ActionLink("Edit", "Edit", new ...

  6. query string parameters 、 Form Data 、 Request Payload

    微信小程序ajax向后台传递参数的时候总是报400错误 然后看了一下network 发现是query string parameters,但是我写的header如下 header:{ "Co ...

  7. springMVC接收参数的区别form data与query string parameters与request payload

    在AJAX请求中,我见过有三种form表单数据类型提交. 第一种:form data, 第二种:query string parameters,第三种:request payload. 在google ...

  8. http 请求参数之Query String Parameters、Form Data、Request Payload

    Query String Parameters 当发起一次GET请求时,参数会以url string的形式进行传递.即?后的字符串则为其请求参数,并以&作为分隔符. 如下http请求报文头: ...

  9. asp.net query string 及 form data 遇到的编码问题

    当遇到此问题时,脑海里闪过的第一个解决方案是设置 web.config 的编码.但一想,就某一个页面的需求而导致其他跟着妥协,不是好的解决方案.于是网上搜索答案,下面做个小分享,遗憾的是研究不够深入, ...

随机推荐

  1. Pandas 时间序列处理

    目录 Pandas 时间序列处理 1 Python 的日期和时间处理 1.1 常用模块 1.2 字符串和 datetime 转换 2 Pandas 的时间处理及操作 2.1 创建与基础操作 2.2 时 ...

  2. js中对象的属性名和属性值

    代码 /** * 对象的属性名 * - 对象的属性名不强制遵循标识符的命名规范,可以是任意的名字,但在开发中 * 尽量遵循标识符的命名规范 */ // 创建对象obj1 var obj1 = new ...

  3. AOS and clustering

    原文转载:http://sjakalax.blogspot.com/2010/10/aos-and-clustering.html AOS and clustering   hi,   There s ...

  4. Oracle package demo 包

    1.package 程序包说明(由函数.过程.变量.常量.游标和异常组成) create or replace package pk_test is -- Author : CHEN -- Creat ...

  5. 2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 题目大意:给定一个含有n个数的序列,还有一个m,对于每个i(1<=i<=n)求出最少 ...

  6. java并发学习--第四章 JDK提供的线程原子性操作工具类

    在了解JDK提供的线程原子性操作工具类之前,我们应该先知道什么是原子性:在多线程并发的条件下,对于变量的操作是线程安全的,不会受到其他线程的干扰.接下来我们就学习JDK中线程的原子性操作. 一.CAS ...

  7. 剖析 Vue.js 内部运行机制 (1)

    1. new Vue() 之后. Vue 会调用 _init 函数进行初始化,也就是这里的 init 过程,它会初始化生命周 期.事件. props. methods. data. computed ...

  8. spring mvc 和spring boot 中注解的使用

    1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...

  9. AppBar中自定义顶部导航

    在上一篇里总结AppBar的一些简单用法,但是AppBar除了有前面那些样式属性外,还能实现类似底部的Tab切换. 首先下载并运行前面的项目: 然后在此基础上实现Tab切换. 常见属性 TabBar有 ...

  10. (转)【mysql元数据库】使用information_schema.tables查询数据库和数据表信息 ---数据记录大小统计

    转:https://www.cnblogs.com/ssslinppp/p/6178636.html https://segmentfault.com/q/1010000007268994?_ea=1 ...