Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

location.href property vs. location.assign() method

Tags:

javascript

Is there any particular advantage/disadvantage in JavaScript memory consumption between using location.href = url as opposed to location.assign(url)?

I guess I'm wondering if it takes more memory to access the method as opposed to setting the property.

like image 560
Doug Weaver Avatar asked Apr 24 '12 17:04

Doug Weaver


People also ask

Why is the location replace () method preferred over the location assign () method?

The replace() method of the Location interface replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History , meaning the user won't be able to use the back button to navigate to it.

What is location assign?

assign() The Location. assign() method causes the window to load and display the document at the URL specified. After the navigation occurs, the user can navigate back to the page that called Location.

What is the use of location href?

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

What is the difference between window location and document location?

window. location is read/write on all compliant browsers. document. location is read-only in Internet Explorer (at least), but read/write in Gecko-based browsers (Firefox, SeaMonkey).


1 Answers

I personally prefer calling the function instead, because calling a function gives me a better impression that something is running and that is not only a value of a variable that is changing.

But probably yes, it may be true that location.href = url; is faster than location.assign(url), although it may depend on the JavaScript engine implementation, see the test I've just created.

like image 178
Pere Avatar answered Sep 21 '22 17:09

Pere