Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Sean Malloy
PLUS
Sean Malloy
Courses Plus Student 4,520 Points

How can I insert the href link so the address bar does not show the actual anchor address? ex: website.com/#my_link

For a single page site, when linking and scrolling within the same page, how do you prevent the browser address from show the actual anchor link id?

If it is a single page site you should be using Javascript:; in the href because you're not really linking out but having the content changed. Maybe look into AJAX, PHP or AngularJS.

1 Answer

Steven Parker
Steven Parker
231,271 Points

If you position the page using JavaScript instead of using the link, the browser address will remain unchanged. There's an element method called scrollIntoView that makes this pretty easy, but it's classified as "experimental" and doesn't work on all browsers. So it might be better to stick with more primitive methods. Example:

yourLink.onclick = e => {
  e.preventDefault();
  //document.getElementById("targetElementID").scrollIntoView();  <- experimental, not recommended
  window.scroll(0, document.getElementById("targetElementID").getBoundingClientRect().top);
};