Reputation: 2496
I need to check if there is sub-page in url?
Something like this
www.example.com
alert ('This is root of website');
www.example.com/about.html
alert('This is not root of website');
How to alert only when if user is root of website, main page?
Upvotes: 1
Views: 1150
Reputation: 21666
Try location
global object. Use its pathname
property:
alert( location.pathname );
So use something like below
if (location.pathname == "/") {
alert("Homepage");
} else {
alert("not homepage");
}
Upvotes: 0
Reputation: 38102
You can use window.location.pathname
to get the pathname of current URL:
if(window.location.pathname.length > 1) {
alert('This is not root of website');
} else {
alert ('This is root of website');
}
Upvotes: 3
Reputation: 842
if(location.pathname.length>1){
alert('This is not root of website');
}
else{
alert ('This is root of website');
}
Upvotes: 0
Reputation: 15393
var url = "www.example.com/about.html";
if(url.split("/")[1] != ""){
// sub url present
}
else{
// not present
}
Upvotes: 0