March 28, 2012

Differenece between window.location and location.href

Question by Rizwan Shahid

I am getting confused what is difference between window.location and location.href both are acting in same way

Answer by Mateusz W

Not really.
window.location is object that holds all information about current document location (host, href, port, protocol etc.).

location.href is shorthand for window.location.href (you call location from global object – window, so this is window.location.href), and this is only string with full url to current website.

They acting the same when you assign url to them – this will redirect to page which you assing, but you can see difference between them, when you open console (firebug, of developer tools) and write window.location and location.href.

Answer by Starx

location.href property returns the entire URL of the current page.

Where as

window.location property represents the currect location of the window object, if you change this you will get redirected.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!