Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect what page you are on JavaScript? [duplicate]

Is there any way to detect what page you are on in JavaScript?

Why I want to know is so that i can detect what page i am on, and then set the style of an element accordingly.

like image 483
Callum Avatar asked Apr 21 '13 16:04

Callum


2 Answers

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

To check for a specific page you would use:

if ( document.URL.includes("homepage.aspx") ) {
    //Code here
}
like image 175
m.edmondson Avatar answered Oct 24 '22 22:10

m.edmondson


Get url of page are you on

location.href

Or

document.URL

Set style to element by id:

document.getElementById('mydiv').style.border = '1px solid black';
like image 43
Roman Nazarkin Avatar answered Oct 24 '22 22:10

Roman Nazarkin