During the process of developing a website, I made a navigation bar in a separate HTML file (nav.html), that i can load into any page I want use AJAX calls in Javascript. The problem is that the script contained in this navbar doesn't run when the navbar is loaded in the actual page. (The navbar however is loaded correctly in the page)
nav.html
<div id= topnav class="topnav">
<...html...>
</div>
<script>
console.log("THIS WON'T GET PRINTED OUT")
</script>
page.html
<html>
<nav id="nav"></nav>
</html>
<script>
loadTopBar()
function loadTopBar(){
var xhr= new XMLHttpRequest();
xhr.open('GET', 'static/getnavbar', true);
xhr.onreadystatechange= function() {
if (this.readyState!==4) return;
if (this.status!==200) return; // or whatever error handling you want
document.getElementById('nav').innerHTML= this.responseText;
};
xhr.send();
}
</script>