26

I have a link on one page that needs to go to a different page, but load to a specific section on that other page.

I have done this before with bootstrap but they take all the 'coding' out of it, so I need to know how to do from scratch. Here is the markup I have based on this link (not the best resource, I know): http://www.w3schools.com/html/html_links.asp

**Page One**
<a href="/academics/page.html#timeline> Click here </a>
**Page I am linking to**
<div id="timeline" name="timeline"> ... </div>

Can I do this with just HTML, or do I need some JavaScript? If I need to do it via JS, it needs to be on the target page, right?

halfer
  • 19,824
  • 17
  • 99
  • 186
ledgeJumper
  • 3,560
  • 14
  • 45
  • 92

6 Answers6

30

I believe the example you've posted is using HTML5, which allows you to jump to any DOM element with the matching ID attribute. To support older browsers, you'll need to change:

<div id="timeline" name="timeline" ...>

To the old format:

<a name="timeline" />

You'll then be able to navigate to /academics/page.html#timeline and jump right to that section.

Also, check out this similar question.

Community
  • 1
  • 1
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
  • 1
    In this case "older browsers" are Netscape 4 and earlier and Internet Explorer 3 and earlier. Nobody needs to support them. On a related note, the anchor element doesn't have an optional end tag. You can't use the `/>` syntax in HTML. – Quentin Aug 16 '15 at 21:30
  • If it's a react app then how to do that ?? – ZebraCoder Sep 19 '22 at 05:14
16

You can simply use

 <a href="directry/filename.html#section5" >click me</a>

to link to a section/id of another page by

doğukan
  • 23,073
  • 13
  • 57
  • 69
mayur nandu
  • 171
  • 1
  • 4
3

To navigate to a section of another page use:

<a href="example.html#example-section">name-of-link</a>

The example.html would be the page you want to go to, and the #example-section would be the name of the id on that page that you want to navigate to.

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
nezza1
  • 83
  • 8
0

To link from a page to another section of the page, I navigate through the page depending on the page's location to the other, at the URL bar, and add the #id. So what I mean;

<a href = "../#the_part_that_you_want">This takes you #the_part_that_you_want at the page before</a>

-1

I tried the above answer - using page.html#ID_name it gave me a 404 page doesn't exist error.

Then instead of using .html, I simply put a slash / before the # and that worked fine. So my example on the sending page between the link tags looks like:

<a href= "http://my website.com/target-page/#El_Chorro">El Chorro</a>

Just use / instead of .html.

Chait
  • 1,052
  • 2
  • 18
  • 30
-3

To link from a page to another section just use

<a href="index.php#firstdiv">my first div</a>
Bilal
  • 2,883
  • 5
  • 37
  • 60