"#" prefix for location.hash?

Y

Yimin Rong

Does anyone know if there are any browsers where you must specify "#"
as a prefix when setting the hash for the location?

For example, the following would move to the intro section of the
document:

window.location.hash = "#intro";

But in the same browser, this would not work:

window.location.hash = "intro";

So far, in the browsers I've tested, the "#" seems to be optional. If
this behaviour is compliant, any references, on-line or otherwise,
would be welcome.

Thanks,

Yimin Rong
 
S

Stuart Palmer

I don't think this is a JS question as you can do hash anchors in html href
code.

I think you will find that if you put a <a name="#hashname"></a> in IE it
will work and in netscape it won't (new versions of NS might work).
IE is more intelligent than netscape so works out bad code and does work.

The answer is, in the <a name="anchorname"></a>, miss out the hash (#) and
this will work in all browsers. Only on the link that goes to that
page/section of the page do you put the hash symbol.

<a href="page.html#intro">link</a>

<a name="intro"></a> Your intro text.

This will work in all browsers.
Hope that helps.

Stu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top