Grasper
Grasper

Reputation: 1313

Jquery prevent link but keep url anchor

I have links that contain href="#video1"

I need to prevent the click executions with

$( "#accordion_video a" ).click(function( event ) {
  event.preventDefault();
});

but when I do that I don't get #video1 in url like this http://example.com/#video1 and I need that for the going back history. And if I don't prevent it the page blank the center part. Any idea?

Demo.

Upvotes: 2

Views: 364

Answers (1)

Apostolis P.
Apostolis P.

Reputation: 522

Maybe this will work for you

$( "#accordion_video a" ).click(function( event ) {
    event.preventDefault();
    history.pushState("", "", window.location.href.split("#")[0] + $(this).attr('href'))
});

edited: use window.location.href.split("#")[0] instead of window.location.href

Upvotes: 1

Related Questions