views:

9161

answers:

4

What's the preferred method to use to change the location of the current web page using JavaScript? I've seen both window.navigate and document.location used. Are there any differences in behavior? Are there differences in browser implementations?

+12  A: 
window.location.href = 'URL';

is the standard implementation for changing the current window's location.

James Skidmore
+1  A: 

There really isn't a difference; there are about 5 different methods of doing it. However, the ones I see most often are document.location and window.location because they're supported by all major browsers. (I've personally never seen window.navigate used in production code, so maybe it doesn't have very good support?)

musicfreak
document.location doesn't work in all browsers. window.location does.
Philippe Leybaert
Hmm, works for me. What browser does it not work in?
musicfreak
+3  A: 

I'd go with window.location = "http://...";. I've been coding cross-browser JavaScript for a few years, and I've never experienced problems using this approach.

window.navigate and window.location.href seems a bit odd to me.

roosteronacid
window.location works, but it's technically incorrect because "location" is an object.
James Skidmore
But everything in JavaScript is an object :)
roosteronacid
+1  A: 

document.location is a (deprecated but still present) read-only string property, replaced by document.url.

location='url path' is the simplest expression to open a new page in the same window.

kennebec