JQuery doesn't load HTML file (in Bootstrap template)

1k views Asked by At

Trying to load Bootstrap nav with jQuery. It doesn't work...

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script>
   $(function(){
     $("#nav").load("nav.html");
   });
</script>  
</head>
<body>
  <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top" id="nav">
  </nav>

And here is nav.html - simplified version. And it's not loaded.

<a class="navbar-brand" href="#">Menu</a>
2

There are 2 answers

0
Johannes On

I would wrap this into $(document).ready(function() { ...yourFunction... }); so that it's executed after the rest of the doc is loaded.

0
Andrii Gorokhovskyi On

So, seems found the issue. I'm using Bootstrap template, where jquery-3.5.1.slim.min.js file is included - which disables Ajax.

Instead I replaced it with full jQuery version:

<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>

And now .load seems to work.