anchors and window.history

T

Timo

Is there any way to jump to an anchor in the current document without
affecting window.history?
Thanks
 
I

Ivo

"Timo" asks
Is there any way to jump to an anchor in the current document without
affecting window.history?

With the IE only method of scrollIntoView(), this is rather easy:

<a href="#jump"
onclick="if(document.getElementById(this.hash.replace(/^#/,''))){
document.getElementById(this.hash.replace(/^#/,'')).scrollIntoView(); return
false; }"
From here</a>

....

<a id="jump">To here.</a>
 
M

McKirahan

Ivo said:
"Timo" asks

With the IE only method of scrollIntoView(), this is rather easy:

<a href="#jump"
onclick="if(document.getElementById(this.hash.replace(/^#/,''))){
document.getElementById(this.hash.replace(/^#/,'')).scrollIntoView(); return
false; }"

...

<a id="jump">To here.</a>


This shorter form seems to work too:

<a href="#jump"
onclick="document.getElementById(this.hash.replace(/^#/,'')).scrollIntoView(
)">From here</a>

Any reason not to use it?
 
I

Ivo

This shorter form seems to work too:
<snip>

You just removed the if(...){ } test, as though it was there for no reason.
But there are some things in life that are just not reliable and this is one
of them. If the referred element does not exist for some reason (the page
did fully load for example, it happens on the best machines) an ugly error
may occur if you do not test for its existence before referring to it. This
is one of the most basic programming principles.
One should also test for the existence of the scrollIntoView() method before
using it (I copied the code from a local application, and forgot about
that). BTW, scrollIntoView() is a nice IE feature, but of course focus()
works on most other browsers too.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top