Created
March 18, 2020 02:02
-
-
Save jiyuujin/1fd545923ba1af9bdd8f1252b122a9b9 to your computer and use it in GitHub Desktop.
pushStateを使って画面遷移する、popStateを使ってブラウザバックを検知する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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