Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

window.location

assign to location, assign to location.href

I wonder which is the recommended style:

  1. location.href = newURL;
  2. location = newURL;

In bookmarklets, I've nearly always seen the first form, except on google reader's subscription bookmarklet which uses the other.

--Tobu 18:50, 3 May 2007 (PDT)

In Gecko they are the same, as far as I can see (assigning to location invokes href setter internally), so choosing one or another is a matter of personal preference. In other browsers - I don't know. --Nickolay 00:53, 4 May 2007 (PDT)
Thanks --Tobu 20:19, 5 May 2007 (PDT)

needs ref to Location definition

The page refers to a Location but where is that type defined? --johnjbarton 22:00, 9 March 2008 (PDT)

It's here --Tobu 04:11, 15 March 2008 (PDT)

? Did you mean to link a url? --johnjbarton 07:54, 29 April 2008 (PDT)

Need additional documentation for same-page URI's

I believe this is the case:  the browser does not reload the page when responding to a uri that is just an anchor.
The default response to clicking on a link of the form <a href="#foo">...</a> is to
     - move to the element with id foo, and
     - add the new page to the browser history
Replace and assign do the first half, but not the second.
In my opinion, failing to reload the page is a [good] feature,
whereas replace's failure to update the browser history is a bug.
Page Notes 25 August 2010

Document Tags and Contributors

 Contributors to this page: Page Notes, Johnjbarton, Tobu, Nickolay
 Last updated by: Page Notes,