mirror of
https://github.com/jonom/silverstripe-betternavigator.git
synced 2024-10-22 14:05:51 +02:00
132a98be68
Use addEventListener to schedule BN initialisation only if document is still loading (#34)
45 lines
1.5 KiB
JavaScript
Executable File
45 lines
1.5 KiB
JavaScript
Executable File
//For reading cookies
|
|
function getCookie(cname) {
|
|
var name = cname + "=";
|
|
var ca = document.cookie.split(';');
|
|
for(var i=0; i<ca.length; i++) {
|
|
var c = ca[i];
|
|
while (c.charAt(0)===' ') {
|
|
c = c.substring(1);
|
|
}
|
|
if (c.indexOf(name) !== -1) {
|
|
return c.substring(name.length,c.length);
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function initialiseBetterNavigator() {
|
|
//Dom elements
|
|
var BetterNavigator = document.getElementById("BetterNavigator");
|
|
var BetterNavigatorStatus = document.getElementById("BetterNavigatorStatus");
|
|
|
|
//Toggle visibility of menu by clicking status
|
|
BetterNavigatorStatus.onclick = function () {
|
|
BetterNavigator.className = BetterNavigator.className === 'collapsed' ? 'open' : 'collapsed';
|
|
//Set cookie to remember state
|
|
document.cookie = "BetterNavigator=" + BetterNavigator.className + ";path=/";
|
|
};
|
|
|
|
//Restore menu state
|
|
if (getCookie('BetterNavigator') === 'open') {
|
|
BetterNavigator.className = 'open';
|
|
}
|
|
}
|
|
|
|
if (document.addEventListener && document.readyState === 'loading') {
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
// wait til DOM finishes loading
|
|
initialiseBetterNavigator();
|
|
});
|
|
} else {
|
|
// This is the case for IE8 and below OR already loaded document (e.g. when using async)
|
|
// initialise straight away - fine if script is loaded after BN dom element
|
|
initialiseBetterNavigator();
|
|
}
|