invertedSpear
invertedSpear

Reputation: 11054

Where can I find documentation to support this behavior?

I'm looking over some previous developers code and I come across this line:

location.href = '#' + variable;

Which has the effect of updating location.hash. Remove the '#' and of course it redirects to the non-existent url. Playing around a bit it seems I can set the hash via location.href as long as the value starts with '#'. This line or similar is used a lot, but I can't seem to find any documentation the supports this behavior of it updating location.hash by setting location.href this way.

I would like to see something showing this isn't just a happy accident that this works so I don't have to re-code all the situations where this is used. Anything you can link me to would help.

Would it be better to just changes these to properly set the location.hash anyway?

Thnks

Upvotes: 1

Views: 50

Answers (4)

millimoose
millimoose

Reputation: 39980

At a guess this is because setting location.href to value is supposed to have the same behaviour as clicking a link whose href=value would; it's not supposed to replace the contents of the address bar, because then you'd have to build absolute URLs everytime you wanted to use location.href.

Assigning values to location and location.href was apparently there back in Javascript 1.0, so it's entirely possible this wasn't properly specified anywhere – neither the Mozilla or Microsoft documentation go into detail. HTML5 specifies the behaviour, most likely retroactively.

Upvotes: 2

mtrovo
mtrovo

Reputation: 879

This URLs with a '#' char are called anchor based URLs, they're not supposed to redirect the user from the page, instead they just update the position of the page by some offset, the same way as setting the location.hash you cited.

As stated by Sii this works because when you change the location.href value it's like you're clicking on a link for example then you have the following equivalence:

<a href="#toc" >Go to Table of Contents</a>

Is the same as:

location.href = "#toc";

Both of them would result in your location.hash variable to have the value toc.

Upvotes: 0

Eineki
Eineki

Reputation: 14959

This is a good place to start your journey on the location properties.

https://developer.mozilla.org/en/window.location

By the way, #something is a valid url and assigning a new url to window.location cause the browser to navigate to the new destination.

#something is called hash and direct the browser to an anchor on the current document, or to the top of the document if the anchor does not exists.

Upvotes: 1

Keith.Abramo
Keith.Abramo

Reputation: 6965

http://docstore.mik.ua/orelly/webprog/DHTML_javascript/0596004672_jvdhtmlckbk-chp-10-sect-2.html

So what happens is when you set location.href to something that is not seen as an absolute path. The browser will automatically put the current url prepended to whatever value you are trying to set it to.

So "#section1" = "www.mysitethatistoocoolforschool.com#section1"

and "section1" = "www.mysitethatistoocoolforschool.comsection1" (this does not exist)

Upvotes: 0

Related Questions