分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

URL地址栏传参与取参

发布时间:2023-09-06 02:28责任编辑:傅花花关键词:暂无标签

URL地址(传参)

js写法:
//1.window.location.hrefvar a ="1018802,8"var b ="1"window.location.href="../EditPosts.aspx?postid="+a+"&update="+b;//2.字符串模板,动态生成href属性var c = ‘<a class=" " href="../EditPosts.aspx?postid=\‘‘+a+‘\‘)">查看地址</a>‘

URL地址(取参)

1.只使用split将字符串截取成数组
var url=window.location.hrefvar pars=url.split("?")[1].split("=")[1].split(",")[1];//例如 https://i.cnblogs.com/EditPosts.aspx?postid=1018802,8&update=1//执行到split("?")[1] ?=> ?"postid=1018802,8&update=1"//执行到split("&")[1] ?=> ?"1018802,8&update"//执行到split(",") ?=> ?["1018802,8","update"]//执行到split(",")[1] ?=> ?"1018802,8"

反复截取,有点繁琐哎~

2.字符串拆分法

window.location.href 或者 location.href 或者 window.location 获得地址栏中的所有内容

decodeURI()可以解码地址栏中的数据 恢复中文数据

window.search 获得地址栏中问号及问号之后的数据

//获取地址栏里(URL)传递的参数 ?function GetRequest(value) { ?????//url例子:www.bicycle.com?id="123456"&Name="bicycle"; ?????var url = decodeURI(location.search); //?id="123456"&Name="bicycle"; ???var object = {}; ???if(url.indexOf("?") != -1)//url中存在问号,也就说有参数。 ?????{ ????????var str = url.substr(1); ?//得到?后面的字符串 ?????var strs = str.split("&"); ?//将得到的参数分隔成数组[id="123456",Name="bicycle"]; ?????for(var i = 0; i < strs.length; i ++) ?????????{ ??        object[strs[i].split("=")[0]]=strs[i].split("=")[1]      }  } ???return object[value]; ?} ?
3.正则匹配法

这种方法其实原理和上一种方法类似,都是从URL中提取,只是提取的方法不同而已。

function GetQueryString(name) { ?????var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); ?????var r = window.location.search.substr(1).match(reg); ?????if (r != null) { ??????????return unescape(r[2]); ?????} ?????return null; ?} 
4.在vue中可以通过this.$route获取路由对象然后根据具体需要取对象内容
this.$route.path 当前页面路由this.$route.params 路由参数this.$route.query 查询路由参数

URL地址栏传参与取参

原文地址:https://www.cnblogs.com/missme-lina/p/10188028.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved