Determine which page you are on javascript on?

Is there any way to determine which page you are in javscript on?

Why I want to know, so that I can determine which page I'm on, and then set the element style accordingly.

+4
source share
2 answers

document.URL will get the URL of the current page.

To check for a specific page, you should use:

 if ( document.URL.contains("homepage.aspx") ) { //Code here } 
+2
source

Get page URL on page

 location.href 

or

 document.URL 

Set the style for an element by id:

 document.getElementById('mydiv').style.border = '1px solid black'; 
+2
source

All Articles