mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-09 04:22:01 +00:00
chore: migrate many of our interactive scripts to defer
This commit is contained in:
26
public/scripts/backbtn.js
Normal file
26
public/scripts/backbtn.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/* AFTER CHANGING THIS FILE, PLEASE MANUALLY MINIFY IT AND PUT INTO backbtn.min.js */
|
||||
/* TODO: Add minifier to build script */
|
||||
window.onload = () => {
|
||||
const backBtn = document.querySelector('#backbtn');
|
||||
|
||||
let hasHistory = false;
|
||||
window.addEventListener('beforeunload', () => {
|
||||
hasHistory = true;
|
||||
})
|
||||
|
||||
backBtn.addEventListener('click', () => {
|
||||
if (!document.referrer) {
|
||||
// This is the first page the user has visited on the site in this session
|
||||
window.location.href = '/';
|
||||
return;
|
||||
}
|
||||
history.back();
|
||||
|
||||
// User cannot go back, meaning that we're at the first page of the site session
|
||||
setTimeout(() => {
|
||||
if (!hasHistory){
|
||||
window.location.href = "/";
|
||||
}
|
||||
}, 200);
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user