views:

52

answers:

4

I'd like for the page to open at a certain div halfway down the page, not at the top...

I have something like:

<div id="d1">
<div id="d2">
<div id="d3">
<div id="d4">
<div id="d5">
<div id="d6">

How can I get the page to open at #d4, instead of the top? (Besides adding #d4 to the end to the URL...)

I imagine there must be some easy way to do this, but I can't figure out how to go at searching for a solution! HTML, javascript? Any help is greatly appreciated.

A: 

You can use Javascript:

location.replace('#d4');
SLaks
He specifically said he didn't want to do that...
Ryley
@Ryley: You misunderstood him. He meant that he doesn't want to load the page from a different URL.
SLaks
Ugh, ok... I still don't really see the difference (reloading it now with #d4 vs loading it initially with #d4), and I mis-clicked on my voting a bunch of times, so can't change it now....
Ryley
@Ryley: I assume that he just doesn't want the page's intial URL (which might be published in a print ad) to have a `#`.
SLaks
Hmm, I gave this a try, I don't mind it adding the anchor to the URL like this (I just don't it to have to be added initially) but it only worked on reload, not on the initial load.
christina
Move the `<script>` block to the end of the page and it should work.
SLaks
A: 

Find div position using this and then use the following javascript command:

window.scroll(0, DIV_POS); // horizontal and vertical scroll targets
Ehsan
Exactly what I was looking for. Thanks!
christina
+1  A: 
<script>
function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;

  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }

 window.scrollTo(selectedPosX,selectedPosY);

}
</script>

http://radio.javaranch.com/pascarello/2005/01/09/1105293729000.html

Orny
awesome link thanks!
christina
A: 

EDIT: OOPS! didn't read the Except.... disregard!

Note to self, read the entire question before responding!

End EDIT

You could always use an HTML anchor tag

<a name="d1" />
<div id="d1"> 
<a name="d2" />
<div id="d2">
<a name="d3" /> 
<div id="d3"> 
<a name="d4" />
<div id="d4"> 
<a name="d5" />
<div id="d5"> 
<a name="d6" />
<div id="d6"> 

When you navigate to the page, you would include the anchor name in the url: pagename.htm#d4

Make sure to close your div tags.

Good luck,

Patrick

Patrick