Skip to content

Instantly share code, notes, and snippets.

@jiyuujin
Created March 18, 2020 02:02
Show Gist options
  • Save jiyuujin/1fd545923ba1af9bdd8f1252b122a9b9 to your computer and use it in GitHub Desktop.
Save jiyuujin/1fd545923ba1af9bdd8f1252b122a9b9 to your computer and use it in GitHub Desktop.
pushStateを使って画面遷移する、popStateを使ってブラウザバックを検知する
if (window.history && window.history.pushState) {
const selectors = [
'.js-double_click_link:not([target="_blank"])',
'.breadcrumb-section a',
'.pagenation-init a',
'a[href*="sort:"]'
];
$(document).on('click', selectors, function(event) {
event.preventDefault();
const url = $(this).attr('href');
history.pushState('', '', url);
console.log('画面遷移した');
});
$(window).on('popstate', function(event) {
if (!event.originalEvent.state) {
return;
}
console.log('ブラウザバックを検知した');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment