url查询参数解析

1.获取url的各部分值

举例http://i.cnblogs.com/EditPosts.aspx?opt=1

1、window.location.href(设置或获取整个 URL 为字符串)

var test = window.location.href;
alert(test);
返回:http://i.cnblogs.com/EditPosts.aspx?opt=1
2、window.location.protocol(设置或获取 URL 的协议部分) var test = window.location.protocol;
alert(test);
返回:http: 3、window.location.host(设置或获取 URL 的主机部分) var test = window.location.host;
alert(test);
返回:i.cnblogs.com 4、window.location.port(设置或获取与 URL 关联的端口号码) var test = window.location.port;
alert(test);
返回:空字符(如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符) 5、window.location.pathname(设置或获取与 URL 的路径部分(就是文件地址))
var test = window.location.pathname;
alert(test);
返回:/EditPosts.aspx 6、window.location.search(设置或获取 href 属性中跟在问号后面的部分) var test = window.location.search;
alert(test);
返回:?opt=1 PS:获得查询(参数)部分,除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值。 7、window.location.hash(设置或获取 href 属性中在井号“#”后面的分段) var test = window.location.hash;
alert(test);
返回:空字符(因为url中没有)

2.将url查询参数通过正则表达式解析成数据字典

function getQueryObject(url) {
url = url == null ? window.location.href : url;
var search = url.substring(url.lastIndexOf("?") + 1);
var obj = {};
var reg = /([^?&=]+)=([^?&=]*)/g;
// [^?&=]+表示:除了?、&、=之外的一到多个字符
// [^?&=]*表示:除了?、&、=之外的0到多个字符(任意多个)
search.replace(reg, function (rs, $1, $2) {
var name = decodeURIComponent($1);
var val = decodeURIComponent($2);
val = String(val);
obj[name] = val;
return rs;
});
return obj;
}
console.log(getQueryObject('https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=12306%E7%81%AB%E8%BD%A6%E7%A5%A8%E7%BD%91%E4%B8%8A%E8%AE%A2%E7%A5%A8%E5%AE%98%E7%BD%91'));
// Object {tn: "monline_3_dg", ie: "utf-8", wd: "12306火车票网上订票官网"}

3.将url查询参数手动解析成数据字典

function getQueryStringArgs(url){
url = url == null ? window.location.href : url;
var qs = url.substring(url.lastIndexOf("?") + 1);
var args = {};
var items = qs.length > 0 ? qs.split('&') : [];
var item = null;
var name = null;
var value = null;
for(var i=0; i<items.length; i++){
item = items[i].split("=");
//用decodeURIComponent()分别解码name 和value(因为查询字符串应该是被编码过的)。
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]); if(name.length){
args[name] = value;
}
} return args;
}
console.log(getQueryStringArgs('https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=12306%E7%81%AB%E8%BD%A6%E7%A5%A8%E7%BD%91%E4%B8%8A%E8%AE%A2%E7%A5%A8%E5%AE%98%E7%BD%91'));
// Object {tn: "monline_3_dg", ie: "utf-8", wd: "12306火车票网上订票官网"}

url查询参数解析的更多相关文章

  1. gin的url查询参数解析

    gin作为go语言最知名的网络库,在这里我简要介绍一下url的查询参数解析.主要是这里面存在一些需要注意的地方.这里,直接给出代码,和运行结果,在必要的地方进行分析. 代码1: type Struct ...

  2. Node基础:url查询参数解析之querystring

    模块概述 在nodejs中,提供了querystring这个模块,用来做url查询参数的解析,使用非常简单. 模块总共有四个方法,绝大部分时,我们只会用到 .parse(). .stringify() ...

  3. 获取url查询参数的方法

    /** * 获取url查询参数的方法 * @param name * @returns {null} * @constructor */ function GetQueryString(name) { ...

  4. angular6 监听url查询参数变化刷新页面

    快照snapshot取到的参数是组件第一次渲染时候的参数,当我们在页面中需要根据不同的url查询参数显示不同的内容时,快照就不能满足我们的需要了,这时候就要用ActivatedRoute服务的quer ...

  5. 函数parseQuery用于解析url查询参数

    在百度上找的,以后忘了再看. 语法如下: var obj = parseQuery(query) query是被解析的查询参数,函数返回解析后的对象. 使用范例如下: var jerry = pars ...

  6. URL网址参数解析类

    /** * Created by myc on 2015/12/9. */ import android.text.TextUtils; import java.util.HashMap; impor ...

  7. react获取url查询参数

    继承自React.Component的this.props.location.query对象下有当前url的各种查询参数.简单的例子:在控制台打印这个对象 import React from 'rea ...

  8. 将url的查询参数解析成字典对象

    1, 这个题目不约而同的出现在了多家公司的面试题中,当然也是因为太过于典型,解决方案无非就是拆字符或者用正则匹配来解决,我个人强烈建议用正则匹配,因为url允许用户随意输入,如果用拆字符的方式,有任何 ...

  9. 一种快速构造和获取URL查询参数的方法:URLSearchParams

    URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串. URLSearchParams()是个构造函数,将返回一个可以操作查询字符串的对象. 常用方法: 1.构造查询字 ...

随机推荐

  1. python 字符转换记录

    1.unicode转utf-8格式: a="unicode格式的字符" a=a.encode("utf-8") 2.utf-8转unicode格式: s2 = ...

  2. JSON 简介

    ylbtech-JSON: JSON 简介 JSON:JavaScript Object Notation(JavaScript 对象表示法) JSON是存储和交换文本信息的语法,类似 XML. JS ...

  3. css中背景的应用

    浏览器默认的字体大小是 font-size:16px; 谷歌最小的是10px 其他浏览器是12px 通配符选择器 *   “*”的意思是代表所有的标签 回到正题 background   背景 他的几 ...

  4. Java zxing生成二维码所需的jar包

    免费的,不需要积分. 共有2个jar包, 链接: https://pan.baidu.com/s/1QJcEkRQOp1NdrNAgGC6LvQ 密码: 4524

  5. [UE4]角度和弧度

    1角度 = 一个圆周的1/360 1弧度 = 长度为半径的弧,其所对的圆心角

  6. vue 动态路由 Get传值

    main.js //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', compone ...

  7. Jensen不等式

  8. pandas中一列含有多种数据类型的转换:科学计算法转浮点数、字符映射

    import pandas as pd import re def getNum(x): """ 科学计数法和字符转浮点数 """ if r ...

  9. Android重打包+重新签名工具Apktool Box

    可实现apk反编译+重新打包+重新签名,界面如下 : 部分引用自开源代码:http://github.com/Bu4275/AutoAPKTool

  10. php namespace use 研究

    1.file1.php: <?php namespace foos; class demo{ function testfn(){ echo "sdlkfjskdjf"; } ...