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. C# 委托 线程 窗体假死

    转载:http://www.cnblogs.com/smartls/archive/2011/04/08/2008981.html 异步调用是CLR为开发者提供的一种重要的编程手段,它也是构建高性能. ...

  2. 又一年NOIP后的一波总结

    (昨天正式考完了吧...先写一下现在的感受,出成绩以及后续继续更...) 按照国际惯例,还是先讲一下故事吧. Day(~,0] 大概是跟随者时间的推进,气氛越来越紧张吧. 平时好像大家和往常一样,日常 ...

  3. 【杂记】mysql 左右连接查询中的NULL的数据筛选问题,查询NULL设置默认值,DATE_FORMAT函数

    MySQL左右连接查询中的NULL的数据筛选问题 xpression 为 Null,则 IsNull 将返回 True:否则 IsNull 将返回 False. 如果 expression 由多个变量 ...

  4. 前端小姐姐学PHP之(一)

    作为一个前端不懂后台那是不对的,嘻嘻,来走一波... 一.安装 **我这里用的是phpStudy和phpStrom** 1.安装phpStudy 链接:https://pan.baidu.com/s/ ...

  5. flask之视图函数从前端接收数据的方法

    一:从前端接收查询字符串  query-string 注意:get和post都可以在url后面添加查询字符串?a=1&b=2 测试工具:postman 1:get方式接收 视图函数 from ...

  6. python 按多维列表中的某一个元素位进行排序

    import os,re top = os.popen("tasklist") process_list = [] split_r = r"\s+" memor ...

  7. vue开发可复用组件

    组件,是一个具有一定功能,且不同组件间功能相对独立的模块.高内聚.低耦合.   开发可复用性的组件应遵循以下原则:   1.规范化命名:组件的命名应该跟业务无关,而是依据组件的功能命名. 2.数据扁平 ...

  8. C++数组读入MATLAB数据

    data = rand(8, 10); fid = fopen('File.data', 'w'); if fid == - 1 error('Cannot open file for writing ...

  9. 【ElasticSearch】概念

    小史是一个非科班的程序员,虽然学的是电子专业,但是通过自己的努力成功通过了面试,现在要开始迎接新生活了. 对小史面试情况感兴趣的同学可以观看面试现场系列. 随着央视诗词大会的热播,小史开始对诗词感兴趣 ...

  10. xcode7 添加个人账户 is not on any development teams

    XCODE7已经可以免费真机测试, 但添加个人账户后,显示 is not on any development teams , 解决办法: 点击 “-” 删除当前账户,退出XCODE重新打开再添加即可 ...