I'm looking to use the html anchor tag to jump to a different page on my site. Is that possible? All info I find only talks about doing it within the same page.

I have a page with options and another page with those options in detail. When they are taken to the second page, I want the option they chose to be at the top of the page, instead of them having to scroll down to find it.


4 answers

o.k.w 2355

You can use hash to navigate to a different page, e.g. from "page1.html" to "page2.html#name1" via a generic anchor tag with the url in the 'href' attribute.

However that will not retain the options from the first page unless you are using:

  • cookies to store/retrieve the options
  • server-side mechanism to store/populate the options
  • javascript to manipulate the url to the second page via querystring or hash
  • form submission or HTTP_POST to the second page
Answered over 9 years ago by o.k.w
danwellman 5600

Sure it's possible, if pageB.html has a series of containers that each have information in them, and an id like info1, info2, info3 etc, then you can set the fragment at the end of each option link on pageA.html like this:

<a href="pageB.html#info1>Info1</a>


So if there are 30 different sets of info on pageB.html and the info30 container would be right at the bottom, if someone clicks the link for info30 on pageA.html when pageB.html loads, info30 will be at the top. It will still be below all of the other info boxes, but the page will automatically be scrolled to the the container id...

The http request will automatically 'know' which info link was clicked from the href. You don't need to implement any state-saving mechanism for this.

See this test page for an example...

Answered over 9 years ago by danwellman
  • Do you happen to know of an example I can follow? So basically it can be done with some css? Foppa over 9 years ago
  • No css needed, when you use a document fragment identifyer with a link, the element that has an id matching the fragment (everything after the #) is automatically at the top of the page. This is true whether the fragment is on the same page, or another page. For example, when you have a link at the bottom of a page that says 'back to top' or similar, the href will be #top (or similar) and when clicked, the page will automatically jump back to the top (an element at the top of the page must have 'top' as an id of course). No JS or special CSS is needed, it is just the way anchors work :) danwellman over 9 years ago
Saiyan 113

you should also read this if you need some more informations about links and anchors in html

Answered over 9 years ago by Saiyan
Foppa 0

So it's not possible at all, with just simple html?

Answered over 9 years ago by Foppa
  • Based on your requirements, not possible with pure generic HTML. o.k.w over 9 years ago
  • OK, if your requirements are like what Dan described in his answer on this page (though I beg to differ), then yes. o.k.w over 9 years ago
  • Just an observationif you have questions about one or more of the answers, it's probably best to add those as a comment to the best answer so far (which would be o.k.w's, in my opinion). Jordan Gray over 9 years ago
  • So the answer saying that it's not possible when actually it is possible (no offense o.k.w) is the best...? Check out http://www.danwellman.co.uk/tests/linktest/pagea.html which has a list of links to 'info sections' as the OP requested. If you click one of the lower down links, e.g. link 9, it will take you to page b where, as if by magic, section 9 will be at the top of the page so you dont have to scroll down to it... danwellman over 9 years ago
  • @Dan, none taken, no worries. What I believe is different interpretations of the question or the actual requirements. I agree with Dan's answer based on how he perceive the question. Mine was assuming the OP wants to retain certain non-trivial states from one page to the other. o.k.w over 9 years ago
  • cool :) danwellman over 9 years ago
  • Absolutely no offense was intended towards your own very clear and helpful answer, Dan; my apologies if it appeared otherwise. My opinion that o.k.w's answer is the best is based on his additional suggestions on how to maintain state (e.g. any options set on the previous page) between requests, a requirement left ambiguous by the question. If there is no need for this, using a vanilla hyperlink is an ideal solution, and o.k.w provides that too. Jordan Gray over 9 years ago