3){ alert('You are in -6ren">
gpt4 book ai didi

javascript - JQuery 检测 If in Homepage 和 Homepage PLUS url 变量

转载 作者:行者123 更新时间:2023-12-03 21:38:05 25 4
gpt4 key购买 nike

我使用此代码来检测主页,效果很好:

var url= window.location.href;
if(url.split("/").length>3){
alert('You are in the homepage');
}

我的问题是我还需要检测网址是否有变量,例如:

mysite.com?variable=something

我还需要检测网址上是否也有变量

我该怎么做?

最佳答案

使用window.location.pathname也可以:

if ( window.location.pathname == '/' ){
// Index (home) page

} else {
// Other page
console.log(window.location.pathname);
}

参见MDN info on window.location.pathname .

关于javascript - JQuery 检测 If in Homepage 和 Homepage PLUS url 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com