js获取url参数值的几种方式
一、原生js获取URL参数值:
比如当前URL为:http://localhost:8080/#/page2?id=100&name=guanxy
<template>
<div>
<div>
<button style="background-color: orange" @click="getUrlParam">方式一:采用正则表达式获取地址栏参数 (代码简洁,重点正则)</button>
<p>结果:id={{method1_id}},name={{method1_name}}</p>
</div>
<div>
<button style="background-color: green" @click="getUrlParam2"> 方法二:split拆分法 (代码较复杂,较易理解)</button>
<p>结果:id={{method2_id}},name={{method2_name}}</p>
</div>
<div>
<button style="background-color: aqua" @click="getUrlParam3"> 方法三:split拆分法(根据参数名获取对应的值)</button>
<p>结果:id={{method3_id}},name={{method3_name}}</p>
</div>
</div>
</template> <script>
export default {
name: "page2",
data() {
return {
method1_id: '',
method1_name: '',
method2_id: '',
method2_name: '',
method3_id: '',
method3_name: '',
}
},
methods: {
getUrlParam() {
//为什么window.location.search取值为空?
//“http://localhost:8080/#/page2?id=100&name=guanxy”那么使用window.location.search得到的就是空(“”)。
// 因为“?id=100&name=guanxy”串字符是属于“#/page2?id=100&name=guanxy”这个串字符的,也就是说查询字符串search只能在取到“?”后面和“#”之前的内容,如果“#”之前没有“?”search取值为空。
this.method1_id = this.getQueryString('id');
this.method1_name = this.getQueryString('name');
},
getQueryString(name) {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let url = window.location.hash.split('?')[1].match(reg);
// console.log(url)
if (url != null) {
return decodeURI(url[2])//decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。
} else {
return null
}
},
getUrlParam2() {
let url = window.location.hash.split('?')[1].split("&");
// console.log(url)
let paramObj = new Object()
url.forEach((item, i) => {
paramObj[item.split('=')[0]] = item.split('=')[1]
})
this.method2_id = paramObj.id,
this.method2_name = paramObj.name
},
getUrlParam3() {
this.method3_id = this.getQueryVariable('id');
this.method3_name = this.getQueryVariable('name')
},
getQueryVariable(variable) {
let url = window.location.hash.split('?')[1].split("&");
for (let i = 0; i < url.length; i++) {
let pair = url[i].split('=');
if (pair[0] == variable) {
return pair[1]
}
}
return false
}
}
}
</script> <style scoped> </style>
页面效果:

二、Vue 获取URL参数值
<p>params获取当前路由参数:id={{this.$route.params.id}},name={{this.$route.params.name}}</p>
<p>query获取当前路由参数:id={{this.$route.query.id}},name={{this.$route.query.name}}</p>
js获取url参数值的几种方式的更多相关文章
- js获取url参数值的两种方式
js获取url参数值的方法有很多,下面也为大家介绍两种. 方法一:正则分析法 function getQueryString(name) { var reg = new RegExp(" ...
- js获取url参数值(HTML之间传值)
<h3>未设置设备: <a href="javascript:addTab('设备列表','PKE_DeviceContent/PKE_DeviceContent.aspx ...
- js获取url参数值
用过的封装好的js获取url问号后的参数的方法: <script> var Request = new Object(); Request = GetRequest();var error ...
- js获取url参数值,js获取其他页面传递而来的值
index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&...... 静态html文件js读取url参数 根据获取html的参数值控制htm ...
- js获取url参数值的方法
index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&...... 静态html文件js读取url参数 根据获取html的参数值控制htm ...
- js获取url参数的两种方法
js获取参数,在以前我都是用正在去拆分,然后获取,这种方式感觉是最简单的 方式1: function QueryString(item) { var sValue=location.search.ma ...
- js获取url参数值的方法总结
1.方式一:通过字符串截取的方式获取参数值: 1).函数一:获取URL中的参数名及参数值的集合 /** * [获取URL中的参数名及参数值的集合] * 示例URL:http://htmlJsTest/ ...
- (转)js获取url参数值
明天有空编辑下 今天做项目遇到js取得url地址问号后面的参数,找了下面的,用着非常好,项目是选项卡样式的,也就是一点击二级分类,底下的同样名字的背景变红(选项卡倍选中) http://www.cnb ...
- js获取url参数值的方式
定义方法: function getParam(paramName) { paramValue = ""; isFound = false; paramName = paramNa ...
随机推荐
- PHP- 搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5输出 ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- sparksql 自定义用户函数(UDF)
自定义用户函数有两种方式,区别:是否使用强类型,参考demo:https://github.com/asker124143222/spark-demo 1.不使用强类型,继承UserDefinedAg ...
- TableStore最佳实践:轻松实现轨迹管理与地理围栏
摘要: 基于TableStore轻松实现亿量级轨迹管理与地理围栏 一.方案背景 轨迹管理系统日常生活中使用非常普遍,如外卖派送轨迹.快递物流流转.车辆定位轨迹等.该场景与地理位置管理类似,核心点与瓶颈 ...
- 重磅发布!阿里云推PostgreSQL 10 高可用版
摘要: 近日,阿里云重磅发布PostgreSQL 10 高可用本地SSD盘版,相比原 9.4 版本又新增了JSONB.BRIN索引.GROUPING SETS/CUBE/ROLLUP.UPSERT等多 ...
- oracle-字符串常用函数
1.拼接字符串 1)可以使用“||”来拼接字符串 -------------------------------------- select '拼接'||'字符串' as str from dual ...
- mybatis中一对多查询collection关联不执行
今天遇到的原因是因为下面红底id没有,导致关联查询没有条件(id字段没传),所以一直没有执行. <?xml version="1.0" encoding="UTF- ...
- 92、R语言分析案例
1.读取数据 > bank=read.table("bank-full.csv",header=TRUE,sep=";") > 2.查看数据结构 & ...
- 46、tensorflow入门初步,手写识别0,1,2,3,4,5,6
1.使用tensorflow的SoftMax函数,对手写数字进行识别 Administrator@SuperComputer MINGW64 ~ $ docker run -it -p 8888:88 ...
- 析构中delete this
查看下面代码如何出错 #include <iostream> using namespace std; class A { public: A() { p = this; } ~A() { ...