【发布时间】:2014-05-06 06:45:31
【问题描述】:
我需要检查url中是否有子页面?
类似的东西
www.example.com
alert ('This is root of website');
www.example.com/about.html
alert('This is not root of website');
只有当用户是网站的root,主页时如何提醒?
【问题讨论】:
标签: jquery indexof window.location
我需要检查url中是否有子页面?
类似的东西
www.example.com
alert ('This is root of website');
www.example.com/about.html
alert('This is not root of website');
只有当用户是网站的root,主页时如何提醒?
【问题讨论】:
标签: jquery indexof window.location
您可以使用window.location.pathname获取当前URL的路径名:
if(window.location.pathname.length > 1) {
alert('This is not root of website');
} else {
alert ('This is root of website');
}
【讨论】:
var url = "www.example.com/about.html";
if(url.split("/")[1] != ""){
// sub url present
}
else{
// not present
}
【讨论】:
if(location.pathname.length>1){
alert('This is not root of website');
}
else{
alert ('This is root of website');
}
【讨论】:
试试location 全局对象。使用它的pathname 属性:
alert( location.pathname );
所以使用类似下面的东西
if (location.pathname == "/") {
alert("Homepage");
} else {
alert("not homepage");
}
【讨论】: