0

Is it possible to prevent selected anchors from adding to the browser's history?

For a site I am working on now, this is a particular issue when I try to prompt back to a previous page. By doing so the anchors repeat in the order they where activated, and I have to click back more than once before returning back to the previous page.

Here's some code I am currently working with. Is there a function I can implement to achieve this result?

<!-- Image with Anchor to Open Lightbox Window -->
<div class="container">
  <a href="#view">
    <div class="pic">
      <img src="https://URLTOPIC.jpg">
    </div>
  </a>
</div>

<!-- Lightbox Prompt with Anchor to Close Window -->
<div class="customlightbox lb-animate" id="view">
  <div class="customlightbox-imgwrap">
    <img class="imgsrc" id="customlightbox-img" src="">
  </div>
</div>
<div id="customlightbox-controls" class="lb-animate">
  <a id="close-customlightbox" class="lb-animate" href="#!"></a>
</div>

Any help would be much appreciated!

nr159
  • 191
  • 1
  • 14

1 Answers1

0

When using a empty anchor, for example <a id="close-customlightbox" class="lb-animate" href="#!"></a> which is used as a button, you can set the href this way that will disable the anchor events <a id="close-customlightbox" class="lb-animate" href="javaScript:void(0);"></a>

You can read more about the javascript URI scheme What does "javascript:void(0)" mean?

For the links you are using to trigger the models, avoid the default behavior for it to be added to the window.history object

document.querySelector('.trigger').addEventLister('click', function(e){
  e.preventDefault();
  //Do your stuff
});
Nicolas M. Pardo
  • 753
  • 1
  • 6
  • 16