JavaScript 获取 URL 地址 (去除 # 号及之后内容)
使用 JavaScript 获取 URL 地址并去除 # 号及之后的内容,可以使用以下代码:
let url = 'http://localhost:8080#/login';
let index = url.indexOf('#');
let newUrl = url.substr(0, index);
console.log(newUrl); // http://localhost:8080
代码解释:
url.indexOf('#'):查找 # 号在字符串中的位置,返回其索引值。url.substr(0, index):从字符串的第一个字符开始截取到 # 号索引位置之前的部分。
最终 newUrl 变量保存的就是去除 # 号及之后内容的 URL 地址。
原文地址: https://www.cveoy.top/t/topic/o89U 著作权归作者所有。请勿转载和采集!