9

I want to link to a section of a dynamic page using the # anchor. Something like this:

<a href=page.php?id=3#section-name>LINK</a>

It didn't work. What is the right way to do it?

I'm not using a direct link, but a redirect like header("Location:page.php?id=3#section-name") from another script.

I have a section named section-name in file page.php. I guess page.php has a problem figuring out the value of the id to process (3 or 3#section-name). I am redirected to page.php which has its content repeated vertically.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bodesam
  • 529
  • 4
  • 9
  • 24
  • 1
    Share the other piece of markup. What does the html look like for the anchor tag you want to scroll to? – James Hill Aug 23 '12 at 16:57
  • Does the page have an element with a name of `section-name`? – j08691 Aug 23 '12 at 17:00
  • 2
    What do you mean "it didn't work?" No browser loading? Directed to a wrong page? Are you missing double quotes or were they removed when you typed them in to the editor? – Anthony Choi Aug 23 '12 at 16:59
  • @Anthony Choi: It probably means the browser doesn't scroll to where "section-name" is in that generated ("dynamic") page. – Peter Mortensen Jul 29 '21 at 14:58

3 Answers3

5

You've only presented half of your code so I can only give a sample of the proper way to do it:

<body> 
    <a name="top"> </a>
    <a href="#top">
        Go To Top Of Page
    </a>     
</body>
James Hill
  • 60,353
  • 20
  • 145
  • 161
5

When using anchor tags, you can target an element by its ID. Browsers will look for the ID before it looks for the name attribute when the link refers to such.

<a href="#section-name>LINK</a> will go directly to <div id="section-name"> if it exists.

Here's an example

Read: HTML Anchors with 'name' or 'id'?

Community
  • 1
  • 1
Vin Burgh
  • 1,399
  • 1
  • 8
  • 14
2

A typical anchor tag works as follows:

A href link tag is written like so:

<a href="anchor_example2.html#a001">Jump to a001</a>

See the #a001 above? That is referencing an id in the HTML page, and it will jump to it if you click this link.

To provide an example of how this id that we would jump to might look on a page, look below.

<li id="a001">text here</li>

Reference

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jasonleonhard
  • 12,047
  • 89
  • 66