我需要检查网址中是否有子网页?
像这样的东西
www.example.com
alert ('This is root of website');
www.example.com/about.html
alert('This is not root of website');
如果用户是网站的主页,主页,该如何提醒?
答案 0 :(得分:2)
您可以使用window.location.pathname
获取当前网址的路径名:
if(window.location.pathname.length > 1) {
alert('This is not root of website');
} else {
alert ('This is root of website');
}
答案 1 :(得分:0)
尝试location
全局对象。使用其pathname
属性:
alert( location.pathname );
所以请使用下面的内容
if (location.pathname == "/") {
alert("Homepage");
} else {
alert("not homepage");
}
答案 2 :(得分:0)
var url = "www.example.com/about.html";
if(url.split("/")[1] != ""){
// sub url present
}
else{
// not present
}
答案 3 :(得分:0)
if(location.pathname.length>1){
alert('This is not root of website');
}
else{
alert ('This is root of website');
}