Schneider
Schneider

Reputation: 2496

Check if there is sub-url in Jquery?

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

Answers (4)

Chankey Pathak
Chankey Pathak

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

Felix
Felix

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

Gaurang s
Gaurang s

Reputation: 842

if(location.pathname.length>1){
alert('This is not root of website');
}
else{
alert ('This is root of website');
}

Upvotes: 0

Sudharsan S
Sudharsan S

Reputation: 15393

var url = "www.example.com/about.html";

if(url.split("/")[1] != ""){

  // sub url present
}

else{

// not present
}

Upvotes: 0

Related Questions