chimerical chimerical - 1 year ago 56
Javascript Question

Javascript: Setting location.href versus location

When would you set

location
to a URL string versus setting
location
?

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


vs

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


Reference: https://developer.mozilla.org/en-US/docs/Web/API/window.location

Answer Source

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 whichever you find clearest.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download