存放于工具 JS 文件中的代码
function getUrlParam(paraName) {
var url = document.location.toString();
//alert(url);
var arrObj = url.split("?");
if (arrObj.length > 1) {
var arrPara = arrObj[1].split("&");
var arr;
for (var i = 0; i < arrPara.length; i++) {
arr = arrPara[i].split("=");
if (arr != null && arr[0] == paraName) {
return arr[1];
}
}
return "";
}
else {
return "";
}
}
这个 函数,可以根据 传入的 名字,来获取 URL 中指定的 名值对的 值
location指示了其所连接对象的url位置。Document和window对象中都有location属性,可以通过window.location和document.location访问。
注意 如果想要获得当前文档的完整url字符串,有四种方式
- document.location
- document.location.href
- document.URL
- document.location.toString()
参考目录
HTML DOM中的Location用法详解
https://www.jianshu.com/p/bd0866de3621
该博客详细介绍了JavaScript中用于从URL中提取指定参数值的函数getUrlParam。通过分析document.location.toString()等方法,展示了如何从URL字符串中拆分并获取名值对。文章适合前端开发者,特别是对URL操作感兴趣的读者。

3536

被折叠的 条评论
为什么被折叠?



