When I try to run this script
window.onpopstate = function(popEvent) {
let urlPath = window.location.pathName;
let slash = page.lastIndexOf("/");
let page = urlPath.substr(slash 1);
let $tab;
if (page = '') {
$tab = $("#tabs .tab:first-of-type");
page = $tab.attr("href");
}
else { $tab = $("#tabs .tab[href=" CSS.escape(page) "]") }
$tab.addClass("current").siblings().removeClass("current");
$.ajax({url: "subpages/" page, function(html) {
$(".append").html(html);
} });
}
I’m getting the error message:
foo.js:38 Uncaught ReferenceError: Cannot access 'page' before initialization at window.onpopstate (foo.js:38:15)
I don’t really get what’s the problem.
Could someone help me out?
CodePudding user response:
This error was caused only by mistyping: page
→ urlPath
window.onpopstate = function(popEvent) {
let urlPath = window.location.pathName;
let slash = urlPath.lastIndexOf("/"); // HERE
let page = urlPath.substr(slash 1);
let $tab;
if (page = '') {
$tab = $("#tabs .tab:first-of-type");
page = $tab.attr("href");
}
else { $tab = $("#tabs .tab[href=" CSS.escape(page) "]") }
$tab.addClass("current").siblings().removeClass("current");
$.ajax({url: "subpages/" page, function(html) {
$(".append").html(html);
} });
}