tags:

views:

361

answers:

2

When would you set window.location to a URL string versus setting window.location.href?

window.location = "http://www.stackoverflow.com";

vs

window.location.href = "http://www.stackoverflow.com"

Reference: https://developer.mozilla.org/En/DOM/Window.location

+2  A: 

You might set location directly because it's slightly shorter. If you're trying to be terse, you can usually omit the window. too.

URL assignments to both location.href and location are defined to work in JavaScript 1.0, back in Netscape 2, and have been implemented in every browser since. So take your pick and use whichver you find clearest.

bobince
+1  A: 

Even if both work, I would use the latter. windows.location is an object, and assigning a string to an object doesn't bode well for readability or maintenance.

psychotik