Difference Location.href or Location.assign

Asked

Viewed 503 times

6

What’s the difference between location.href = url and the location.assign(url)? Is it something in Javascript memory consumption? Is there any official recommendation which one to use?

I consider the Location.assign more elegant and readable, but I hope it consumes more memory.

2 answers

6


There is no difference - except that href is a property, and thus also allows the reading of its value, while the method assign only allows the definition of this value (and consequently the loading of a new page).

Performance doesn’t usually need to be a concern unless you’re actually seeing some bottleneck in the application. In this specific case, the use of memory is irrelevant, since after changing the URL the page will be reloaded and the memory used by the JS will be reset.

  • 3

    From what I read, the assign may issue the exception SecurityError in some situations and the href does not care about it. I could not understand right in which situations it would happen. Know something about it?

  • 1

    I read that he can issue this exception, but found no reference of what to use location.href as Setter would not do the same. There is a link saying that href does not care about this?

  • I read that href is a little, a little, a little faster. But I still find it very strange in the middle of the code you give a = and it change your page, a function do this is something much more logical for me.

  • 1

    I believe I misunderstood the first reading of the item 5.5.3.1 Security, that would be an exception to SecurityError. Now I don’t even know if I understood what I read kkk

  • 1

    I read that too (in the OS in English), but if the difference is not glaring it may be in the margin of error of the test. It makes sense your reasoning, if you think it clearer you can use the assign no problem. @Luizsantos

  • 2

    @Andersoncarloswoss I think it’s the implementation of CORS control. I keep believing that both end up calling the same algorithm.

  • 1

    @Luizsantos the location.href is a variable. If you change the variable, theoretically you change what it should show.

  • 1

    For now I agree with you. The afternoon I will read more calmly and check all these contextual nomenclatures involved. At the time it got confused for me.

  • I ended up using href. I just commented that I find the other more legible. The question was also whether one can be discontinued or something like that. Thank you all very much! :)

Show 4 more comments

4

When you use Location.assign it loads that content from the url, but when you use Location.href it works like a traditional link, so it’s better to do anchors 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 all URL.

Location.()
Load the resource into the given URL as parameter.

Source: https://developer.mozilla.org/en-US/docs/Web/API/Location

  • 1

    Actually I don’t think either of them works for anchors.

  • 1

    This one works on W3 https://www.w3schools.com/jsref/prop_loc_href.asp

  • Really, there must have been something wrong with the test I did. assign also works

  • And don’t reload the page if I pass an anchor

  • 2

    Remember that W3schools and W3C are different things. Although the idea of W3schools is to present W3C in a more didactic way, the safest is to use the W3C page itself as a reference. Or even the WHATWG page. About W3schools at the goal...

  • I didn’t know about this separation of W3 and w3c.

  • 1

    To make it easier, always look for the reference in MDN - it alone is already much more reliable than W3schools; but mainly there is always at the bottom of the page a table with the links to the specifications WHATWG and W3C. You can use MDN for a more didactic view of the context and specifications for more technical details.

  • I made a new edition taking into account the comments.

  • 1

    But it seems that you are still saying that they behave differently. Or not?

  • But in my view they are different, "It is a Domstring that contains every URL", what I understood by that was, it has only the url, and as it is used in "Location" it calls the page, "Loads the resource in the given URL as parameter." now if you use the assign it will actually take the content of the url and play on the screen.

  • So much so that if you do this Alert(Location.href = "https://www.w3schools.com"), it will show the url, but if you do this: Alert(Location.assign("https://www.w3schools.com"), it will return indefino.

  • location.href = "w3schools.com" returns href itself and the method call returns Undefined. I keep finding either set with Location.href or assign gives exactly the same result...

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.