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 的编码.但一想,就某一个页面的需求而导致其他跟着妥协,不是好的解决方案.于是网上搜索答案,下面做个小分享,遗憾的是研究不够深入, ...
随机推荐
- C# ASP.NET 手写板并生成图片保存
前端: @{ Layout = null; } <!DOCTYPE html> <html lang="zh-CN"> <head> <t ...
- 自制悬浮框,愉快地查看栈顶 Activity
接手陌生模块时,如何快速了解每个页面对应的类,以及它们之间的跳转逻辑.总不能在代码里一个一个地找startActivity()吧? 有时候,又想查看别人的 app 的页面组织(像淘宝.微信啊),总不能 ...
- Mysql查询结果导出Excel表
Mysql查询结果导出Excel表: 一句转换方式:$ mysql -uops -p'GCNgH000KP' dtbs -e 'select * from t_proxy__record;' --de ...
- shell 搜索指定目录下所有 jar 文件生成csv文件
虽说比较简单,但希望分享给大家.按需求改成想找的:例如txt,xls 等. 脚本名 扫描的路径 文件名 testFind.sh / testFind.txt (如果未配置环境变量 ./testFi ...
- Axios跨域实例
//创建axios实例 var instance = axios.create({ baseURL : "http://localhost:8080", withCredentia ...
- 【Database】Mysql分布式集群学习笔记
一.sql 的基本操作 (2018年11月29日,笔记) (1)数据库相关操作 创建数据库.查看数据库.删除数据库 #. 创建数据库 create database mytest default ch ...
- 线程工具类 - Semaphore(信号量)
Semaphore官方文档 一.使用信号量实现线程间的通信 /** * Demo:使用信号量实现线程间通信*/ public class SemaphoreDemo { public static v ...
- flask之模板之继承
一:继承 基类模板base.html 中在进行挖坑 {% block 坑的名字%}{% endblock %} 子类模板test.html 中 通过 {% extends "base.ht ...
- Java缓冲字符读取
public class BufferedReaderDemo { public static void main(String[] args) throws IOException { // 创建流 ...
- gitHub pull Request记录
1.fork开源项目到自己的gitHub,点fork,然后clone即可 2.提交本地修改,push到自己的代码库 3.点new pull Request,写点备忘信息 注意确保修改的正确性,如果运行 ...