View Full Version : Jump to anchor tag onload

Aug 10th, 2009, 11:01 PM
Hello all,

I needs some help. I currently have a script to jump to an anchor tag when the page loads:

<script type="text/javascript">

function goToAnchor() {
location.href = "myPage.html#myAnchor";


Now, I would like to take it a step further and would like to link from page A to page B that usually takes a while to load and once the page loads, run a script to jump to the specified anchor tag on the page after everything has loaded. I know I have to add some parameters at the end of the url link on page A, but that's where I get stuck.

Can anyone please help?

Old Pedant
Aug 11th, 2009, 12:21 AM
I'm a little lost here.

You mean you want to *ACTIVATE* an <A> tag after the page loads? So that you then jump to the next page??

Or you just mean you want to invoke your current gotoAnchor() function only after the page is fully loaded???

If the latter, you could just do:

<body onload="location.href='myPage.html#myAnchor';">

Aug 11th, 2009, 12:40 AM
Basically, I want to rewrite this code and have it read the url in the browser as "http://www.yoururl.com#anchor_tag". When it detects the "#anchor_tag" in the url, it jumps to the specified anchor tag after the page finish loading. and if it does not detect that "#anchor_tag" in the url, it does nothing and loads normally.

Old Pedant
Aug 11th, 2009, 01:22 AM
Well, you could do it thus:

Use a URL such as:


and then you could use a bit of JavaScript coding to convert it into a local jump, instead, at onload.


<body onload="location.href='somepage.html#' + location.search.substring(1);">

Untested, but I can't see why it wouldn't work.

Aug 12th, 2009, 01:57 PM
Thanks for your help! but I think I came up with a solution. Here it is:

Page A:

<a href="pageB.html#anchor">Click</a>

Page B:

<script type="text/javascript">
function load() {
var urllocation = location.href;
if(urllocation.indexOf("#anchor") > -1){
} else {
return false;

<body onload="load()">

Aug 13th, 2009, 04:58 AM
Most (probably all) browsers do this automatically... for example...

Goes straight to this post. :P

Aug 13th, 2009, 11:34 AM
first add this to the <body> tag of your html page:

<body onload="goToAnchor();">

This will call the function goToAnchor when the page loads. Next you will need to include the function goToAnchor.

<script type="text/javascript">

function goToAnchor() {
location.href = "myPage.html#myAnchor";


Change myPage.html and #myAnchor to your needs. Then all you need is the anchor that you want it to jump to, when the page is loaded.

<a name="myAnchor"></a>