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. 【vue】父子组件间通信----传函数

    (一)子组件 调用 父组件 方法 方式一) 子组件中通过this.$parent.event来调用父组件的方法 父组件 <template> <div> <child&g ...

  2. 模块学习笔记-IR2110/IR2130(上)

    引言 IR2110 / IR2113是高压,高速功率MOSFET和IGBT驱动器,具有独立的高端和低端参考输出通道.逻辑输入与标准CMOS或LSTTL输出兼容,低至3.3V逻辑.浮动通道可用于驱动高端 ...

  3. TensorFlow——MNIST手写数字识别

    MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/   一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...

  4. python内存分析

    安装 首先安装memory_profiler和psutil pip install memory_profiler pip install psutil 在需要分析的函数前面添加装饰器@profile ...

  5. Nginx优化_压缩处理与内存缓存

    对页面进行压缩处理; 服务器内存缓存. 1.对页面进行压缩处理 [root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf http { ... gzip ...

  6. ES基本原理

    Elasticsearch是基于一款高性能的.可扩展的信息检索工具库Lucene构建的强大的搜索引擎,在很多情况,它也被作为NoSql数据库并取得了很好的效果,下面介绍下ES的基本概念,映射到数据库的 ...

  7. uboot tag存储主要部分代码

    https://www.cnblogs.com/pokerface/p/5217106.html     cmd_bootm.c //传递给内核的参数      int do_bootm (cmd_t ...

  8. 【串线篇】spring boot日志框架

    一.日志框架 小张:开发一个大型系统: 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? 2.框架来记录系统的一些运行时信息:日志框架 ...

  9. div+css做出带三角的弹出框 和箭头

    一.三角形 https://blog.csdn.net/Szu_AKer/article/details/51755821 notice:三角的那部分可以用图片作为背景,但是容易出现杂边.所以利用cs ...

  10. CentOS安装Git服务器 Centos 6.5 + Git 1.7.1.0 + gitosis

    1.安装扩展 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel 2.下载git ...