Difference location.href or location.assign

6

What is the difference between location.href = url and location.assign(url) ? Is it something in JavaScript memory consumption? Is there any official recommendation to use?

I consider the location.assign more elegant and readable, but I think it consumes more memory.

    
asked by anonymous 17.10.2017 / 14:48

2 answers

6

There is no difference - except that href is a property, so it also allows you to read its value, whereas the assign method only allows setting this value (and therefore loading a new page).

Performance usually does not have to be a concern unless you are actually seeing some bottleneck in the application. In this specific case, the memory usage is irrelevant, since after the URL exchange the page will be reloaded and the memory used by JS will be reset.

    
17.10.2017 / 15:00
4

When you use the location.assign it loads that content from the url, but when you use the location.href it works like a traditional link, so it's best to do anchor with href:

location.href = "#top";

And to load a new page location.assign:

location.assign("https://www.pagina.com");

Update ---------------------------------------------- -------------------------------------

URLUtils.href
It is a DOMString that contains the entire URL.

Location.assign ()
Loads the resource in the URL provided as a parameter.

Source: link

    
17.10.2017 / 15:02