Javascript only loads when logged in
-
I have added custom javascript to move the right sidebar to the left side of the page on page load. It works beautifully… when I’m logged in to the site.
However, when I’m logged out and view the site, the javascript does not execute at all. No errors, no console logs, no nothing… when I look at sources in dev tools, I can see the javascript file doesn’t even load!
Here is my code (again, it works great as long as I’m logged in):
/** * This function moves the sidebar to the left side of the page. */ window.onload = function () { console.log('EXECUTE THIS'); const contentWrapper = document.getElementById('content-wrapper'); console.log('Children: ' + contentWrapper.children.length); const firstChild = contentWrapper.children[0]; const secondChild = contentWrapper.children[1]; console.log('Will We Go In???'); if (secondChild.id === 'sidebar') { console.log('YUP!') contentWrapper.removeChild(secondChild); contentWrapper.insertBefore(secondChild, firstChild); } };
Here is a screenshot showing the file loaded while logged in:
https://i.stack.imgur.com/52um2.pngAnd here is a screenshot showing the file missing when not logged in:
https://i.stack.imgur.com/OU2bP.pngWhy is it only showing up for logged in users?
I have also asked this question on Stackoverflow:
https://stackoverflow.com/questions/69776450/wordpress-custom-javascript-doesnt-execute-when-logged-out
- The topic ‘Javascript only loads when logged in’ is closed to new replies.