chore: migrate many of our interactive scripts to defer

This commit is contained in:
Corbin Crutchley
2022-09-25 05:50:52 -07:00
parent 51a4ad0fda
commit b5058b458a
9 changed files with 227 additions and 199 deletions

26
public/scripts/backbtn.js Normal file
View 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);
})
}