是否有任何方法可以在没有查询字符串的情况下获取URL
Is there any method to get the URL without query string?
我有一个类似http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235
的URL。
我想获得没有查询字符串的URL: http://localhost/dms/mduserSecurity/UIL/index.php
。
在JavaScript中有任何方法吗?目前我正在使用document.location.href
,但它返回完整的URL。
试试这个:
let path = window.location.href.split('?')[0]
console.log({path})
了解Window.location
和Location
接口:
const urlPieces = [location.protocol, '//', location.host, location.pathname]
let url = urlPieces.join('')
console.log({urlPieces, url})
location.toString().replace(location.search, "")
var url = window.location.origin + window.location.pathname;
如果您也想删除散列,请尝试这个:window.location.href.split(/[?#]/)[0]
下面是使用URL()接口的方法:
new URL(location.pathname, location.href).href
尝试:
document.location.protocol + '//' +
document.location.host +
document.location.pathname;
(注意:.host
而不是.hostname
,以便端口也包括在内,如果必要的话)
获取URL的所有部分,除了查询:
var url = (location.origin).concat(location.pathname).concat(location.hash);
注意,如果有哈希的话,这也包括哈希(我知道示例URL中没有哈希,但为了完整性,我包括了这个方面)。要消除散列,只需排除.concat(location.hash)
。
使用concat
将Javascript字符串连接在一起(而不是+
)是更好的实践:在某些情况下,它可以避免类型混淆等问题。
使用split(最简单的方法)剪切字符串:
var myString = "http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235"
var mySplitResult = myString.split("?");
alert(mySplitResult[0]);
使用window.location
的属性
var loc = window.location;
var withoutQuery = loc.hostname + loc.pathname;
var includingProtocol = loc.protocol + "//" + loc.hostname + loc.pathname;
您可以在https://developer.mozilla.org/en/DOM/window.location
location.href.slice(0, - ((location.search + location.hash).length))
有两种方法:
<script type="text/javascript">
var s="http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu
=true&pcode=1235";
var st=s.substring(0, s.indexOf("?"));
alert(st);
alert(s.replace(/'?.*/,''));
</script>
您可以像这样使用URL构造函数:
const href = 'http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235'; // document.location.href
const url = new URL(href);
const noSearchUrl = href.replace(url.search, '');
console.log(noSearchUrl);
您可以创建URL的实例,然后清除查询字符串:
const url = new URL(document.location.href);
url.search = '';
console.log(url.href);
将这两行添加到$(document)中。
$(document).ready(function () {
$("div.sidebar nav a").removeClass("active");
$('nav a[href$="'+ window.location.pathname.split("?")[0] +'"]').addClass('active');
});
最好使用美元符号($)(以)
$('nav a[href$
代替(^)(以)开头
$('nav a[href^
因为,如果你使用(^)符号并且你在导航菜单中嵌套了url,(例如"/account"answers"/account/roles")
window.location.href.split("#")[0].split("?")[0]
2023年我会做什么:
const windowUrl = window.location.href;
const { origin, pathname } = new URL(windowUrl);
const urlWithoutQueryOrHash = `${origin}${pathname}`;
如果您正在使用导航栏,并希望在单击侧边栏导航后获得纯url,那么以下代码可能会有所帮助:
$(document).ready(function () {
$("div.sidebar nav a").removeClass("active");
var urlPath = window.location.pathname.split("?")[0];
var nav = $('div.sidebar nav a').filter(function () {
return $(this).attr('href').toLowerCase().indexOf(urlPath.toLocaleLowerCase()) > -1;
});
$(nav).each(function () {
if ($(this).attr("href").toLowerCase() == urlPath.toLocaleLowerCase())
$(this).addClass('active');
});
});
- 如何在不创建格式错误的HTML标记的情况下分解字符串
- 如何在不打断标记的情况下突出显示html字符串中的文本
- 在不使用循环的情况下,从一个数据库字符串值向javascript数组添加多个对象
- 如何避免JSON.stringify在特殊情况下返回undefined,从而为JSON.parse创建字符串失败
- 如何通过JS在不干扰其标记的情况下更改HTML文档中字符串的所有实例
- Javascript alert()在没有const字符串的情况下不显示
- 在这种情况下,如何将字符串添加到函数中 php
- 在不更改 HTML 的情况下操作 HTML 字符串的内容
- 限制字符串中显示的字符数,但在没有php的情况下以模态显示同一字符串中的所有字符
- 通过位置栏更改查询字符串,AJAX加载的菜单不会't在没有硬刷新的情况下重新填充
- 在保持字符串同等有效的情况下对其进行视觉加扰
- 如何在没有javascript中的split函数的情况下将字符串拆分为单词
- 在这种情况下,我如何在jquery中将字符串放入php中
- 在不渲染的情况下计算字符串大小
- 在不使用HTML的情况下对字符串中的换行符进行编码
- jQuery:在没有字符串选择器的情况下分离
- 在不使用AJAX的情况下将JSON字符串发送到php文件
- 如何在没有查询字符串和参数的情况下加载jQuery
- 在没有科学记数法的情况下将大数字转换为字符串
- 在 JavaScript 中给定时区字符串的情况下计算 UTC 偏移量