follow a link ('soft')

J

Joris Gillis

Hi everyone,

I have this nasty little problem to which I can't find a solution:

Consider an anchor <a name="Top" id="Top">Top</a>

How can I follow/execute this link from within javascript? In other words, how can I mimic the action that occurs when a user has clicked a (non-existing) <a href="#Top">click me</a> link, so that the page will be shifted to the top (that's an example - not the actual goal)

The link must be followed in the 'softest' possible manner: no opening of new window or page reloads/refreshes.

something like: document.getElementById('Top').follow()

Any help is greatly appreciated.
 
R

RobG

Joris said:
Hi everyone,

I have this nasty little problem to which I can't find a solution:

Consider an anchor <a name="Top" id="Top">Top</a>

How can I follow/execute this link from within javascript? In other
words, how can I mimic the action that occurs when a user has clicked a
(non-existing) <a href="#Top">click me</a> link, so that the page will
be shifted to the top (that's an example - not the actual goal)

The link must be followed in the 'softest' possible manner: no opening
of new window or page reloads/refreshes.

something like: document.getElementById('Top').follow()

The following script modifies the window.location to go to a
specified anchor. The anchor does not have to be visible, it just
has to exist.

Note that you can't simply append the new anchor to the current URL,
so the script removes any pattern matching #.* in the string of
window.location, then adds the new anchor. This is a bit simplistic,
you may need a more robust solution.

I've wrapped lines to prevent autowrapping so the onclick looks a bit
strange.


<a href="#pageFoot" name="pageTop">Normal anchor - go to foot</a>
<br>
<input type="button" value="JS function - go to foot" onclick="
window.location =
window.location.toString().replace(/\#.*/,'') + '#pageFoot';
">
<br>
<div style="position:absolute; top:2000px; border:1px solid blue;">
<input type="button" value="JS function - go to top" onclick="
window.location =
window.location.toString().replace(/\#.*/,'') + '#pageTop';
">
<br>
 
M

Martin Honnen

Joris said:
I have this nasty little problem to which I can't find a solution:

Consider an anchor <a name="Top" id="Top">Top</a>

How can I follow/execute this link from within javascript?

It is an anchor not a link but you can move to an anchor with
window.location.hash = '#Top';
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top