Adding an anchor in the end of the URL automatically?

Discussion in 'JavaScript' started by x0x, Jan 15, 2009.

  1. #1
    I have many backlinks to page.php, I can't edit all those other pages to link to page.php#text

    Is there a way to add #text in the end of the URL if the page is visited? I need to make the page scroll to a certain spot. Thanks!
     
    x0x, Jan 15, 2009 IP
  2. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi x0x,

    here is some code to do what you want..

    
    <script type="text/javascript">
    	function jumptoanchor( anchor )
    	{
    	
    		document.location='#'+anchor;
    	}
    </script>
    
    HTML:
    this function takes as its single parameter the name of the anchor you have set in your page, and when invoked it will scroll to it. (just as if you had clicked on a link pointing there..)

    so add an anchor to your page like
    
    <a name="sectiontoshow">some text</a>
    
    HTML:
    and then call the function as
    
    <body onload="jumptoanchor('sectiontoshow')">
    
    HTML:
    (this is the simplified way to call it ... for more complex sites that other things happen on the onload event there are more correct ways to call the function..)

    hope this helps..
     
    gnp, Jan 15, 2009 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thank you. That's exactly what I was looking for.
     
    x0x, Jan 15, 2009 IP