Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  8] [ 2]  / answers: 1 / hits: 76824  / 11 Years ago, wed, october 23, 2013, 12:00:00

How do I get Javascript to tell me the website url.



For example if I have a page www.example.com/page.html



I want Javascript to tell me the site url is www.example.com and not www.example.com/page.html (which document.location tells me)



Is there a way to do this? If so, how?



Thanks in advance for your help :)


More From » javascript

 Answers
13

There are several ways you can do this, but one way might be best for certain situations (e.g. within an iFrame).




Protocol + Domain + Page



document.URL
> http://example.com/page1.html

document.location.href
> http://example.com/page1.html





Protocol + Domain



document.location.origin
> http://example.com





Domain



document.location.host
> example.com





Page



document.location.pathname
> /page1.html

[#74777] Tuesday, October 22, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ernest

Total Points: 332
Total Questions: 92
Total Answers: 98

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;