Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current browser URL using JavaScript?

Tags:

javascript

I have a website on a host example.myhost.com but I also have a sub-domain for French fr.example.myhost.com which is different than example.myhost.com.

If I'm at fr.example.myhost.com and I use window.location.href, I get example.myhost.com instead of fr.example.myhost.com.

How do I get the URL typed in the address bar of the browser?

like image 481
Alon Avatar asked Feb 05 '12 22:02

Alon


People also ask

How do I get the current URL in HTML?

The current URL can be obtained by using the 'URL' property of the Document object which contains information about the current URL. The 'URL' property returns a string with the full location of the current page, along with containing the string having the HTTP protocol such as ( http://).

How do I get the current page number in JavaScript?

var lighboxHeight = (pagenumber-1)*window.

How do I find the URL of a string?

checkValidity() method is used to check if a string in <input> element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL.


3 Answers

window.location

The subdomain shouldn't make a difference. If, however, you've typed the URL in the address bar, after the page has loaded (but not hit enter), then that's a different story altogether.

like image 189
vol7ron Avatar answered Oct 01 '22 06:10

vol7ron


That's weird, cause it works for me. You might try:

  • window.location.host
  • window.location.hostname
  • window.location.origin
like image 20
LordZardeck Avatar answered Oct 01 '22 06:10

LordZardeck


Look the same question here : Get current URL in web browser

Use :

console.log(document.URL);
like image 44
Ema.H Avatar answered Oct 01 '22 06:10

Ema.H