Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Setting location.href versus location

Tags:

javascript

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

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

vs

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

Mozilla Developer Network Reference

like image 582
chimerical Avatar asked Mar 04 '10 23:03

chimerical


People also ask

What does setting window location href do?

The Location href property in HTML is used to set or return the complete URL of the current page. The Location href property can also be used to set the href value point to another website or point to an email address.

What is location HREF in Javascript?

The location. href property sets or returns the entire URL of the current page.


1 Answers

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.

like image 107
bobince Avatar answered Oct 14 '22 13:10

bobince