Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lukewang1024/45b52186d053bc5f1d37bb39bcc57c00 to your computer and use it in GitHub Desktop.
Save lukewang1024/45b52186d053bc5f1d37bb39bcc57c00 to your computer and use it in GitHub Desktop.
Some useful snippets for frontend web development

Detect if the page load is from a back or forward browser navigation.

if (window.performance) {
  const navEntries = window.performance.getEntriesByType('navigation');
  if (navEntries.length > 0 && navEntries[0].type === 'back_forward') {
    console.log('As per API lv2, this page is load from back/forward');
  } else if (
    window.performance.navigation &&
    window.performance.navigation.type === window.performance.navigation.TYPE_BACK_FORWARD
  ) {
    console.log('As per API lv1, this page is load from back/forward');
  } else {
    console.log('This is normal page load');
  }
} else {
  console.log("Unfortunately, your browser doesn't support this API");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment