Is there a way to ask JavaScript what web page is currently loaded?
Example:
if(page loaded == index.php){
do something
}
else if(page loaded == contact.php){
do something else
}
Thanks
Is there a way to ask JavaScript what web page is currently loaded?
Example:
if(page loaded == index.php){
do something
}
else if(page loaded == contact.php){
do something else
}
Thanks
Share Improve this question asked Nov 12, 2011 at 6:21 Oliver JonesOliver Jones 1,4507 gold badges30 silver badges43 bronze badges4 Answers
Reset to default 9var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if(sPage == "index.php"){
do something
}
else if(sPage == "contact.php"){
do something else
}
You could look at window.location.pathname
, that will give you this:
"/questions/8102940/javascript-check-what-page-has-loaded"
for this question page.
You can check document.URL
to find out what page is loaded.
You could use document.URL.includes. Then it will be something like this:
if(document.URL.includes("index.php")){
do something
}
else if(document.URL.includes("contact.php")){
do something else
}
I know this is a bit late, but i hope this helps for other people.