Solved

Use onLoad function to 'jump' to an internal section (bookmark) on the page.

Posted on 2003-10-24
10
1,068 Views
Last Modified: 2010-05-18
Hello,

If I had a HTML page like the following:

<html>
<head>
</head>
<body>
<table height="100">
  <tr>
    <td>blah blah</td>
  </tr>
</table>
<table>
  <tr>
    <td><a name="image"></a><img src="/images/image.jpg" border="0"></td>
  </tr>
</table>
</body>
</html>

How would I use JavaScript to cause the page to 'jump' down to the image when the page loads? Hopefully my question makes sence ;)

Thanks,
Eric
0
Comment
Question by:emartin24
10 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 9618851
not sure if this will work for all browsers.

<body onload="document.anchors['image'].focus();">
0
 
LVL 30

Accepted Solution

by:
third earned 125 total points
ID: 9618852
try this,

<html>
<head>
</head>
<body onload="window.location='#image';">
<table height="100">
  <tr>
    <td>blah blah</td>
  </tr>
</table>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<table>
  <tr>
    <td><a  name="image"></a><img src="/images/image.jpg" border="0"></td>
  </tr>
</table>
</body>
</html>
0
 
LVL 7

Expert Comment

by:Xxavier
ID: 9618862
  Why not just use anchors  ie the page is called with mypage.htm#image. Or on the onload event put document.location=mypage.htm#image which will scroll down when the page is opened as just mypage.htm
0
 
LVL 10

Expert Comment

by:Nushi
ID: 9619273
i must agree with the last comment:

use onload="document.location.replace('mypage.htm#image')";
this is the best solution.


Nushi.
0
 
LVL 25

Expert Comment

by:devic
ID: 9619359
with scroollTo too:
===============================
<body onload=scrollTo(0,1500)>
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:superslamwich
ID: 9619441
except that onload="document.location.replace('mypage.htm#image') will create an infinite loop of loading and reloading the page.  Use this instead:

<script type='text/javascript'>
<!--
if(document.location.search=="")  {
  document.location.replace(document.location + "#myimage")
}
//-->
</script>


{Slam}
0
 
LVL 3

Expert Comment

by:superslamwich
ID: 9619444
Whoops, cancel that comment.  If you use an you won't create the loop...
0
 
LVL 3

Expert Comment

by:superslamwich
ID: 9619447
*If you use an anchor you won't create the loop
0
 

Author Comment

by:emartin24
ID: 9619652
I will try some of the solutions mentioned. Maybe I should have mentioned this before ;), but the reason I don't load the page with mypage.htm#image is because the output is coming from a CGI script.

So the URL is:
http://www.mysite.com/cgi-bin/myprogram.cgi?image=myimage.jpg

I do not think that it works to use:
http://www.mysite.com/cgi-bin/myprogram.cgi?image=myimage.jpg#image

Which is why I was thinking I could use javascript to have the page go directly to the image when it loads. And the reason for this is to prevent having to scroll down to see the image.

Thanks.
0
 

Author Comment

by:emartin24
ID: 9619755
third's suggestion did the trick, so I have accepted his answer. Thank you all for your contributions.

Thanks,
Eric
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now