最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

load - JavaScript - Check What Page Has Loaded? - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 9
var 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.

发布评论

评论列表(0)

  1. 暂无评论