-2

I'm trying to use jquery to simply load another html file in my current html file, but nothing is showing. I tried following one of the answers here, but I'm still not seeing anything appear on my html file.

about.html

<!DOCTYPE html>
<html>

  <head>
    <script src="http://code.jquery.com/jquery-2.1.4.js">
    $(function(){
      $("#topbar").load("b.html");
    });
    </script>
  </head>

  <body>
    <div id="topbar"></div>
  </body>
</html>

b.html

<p> This is the included file </p>

what am I doing wrong here?

Community
  • 1
  • 1
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

2 Answers2

7

JS <script> 101: You can either have a fully defined script inside the <script>...</script> block, OR you can load the script from an external src. You cannot have both in a single block. If you specify a src, then the body of the <script> block is ignored. You should have

<script src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script> <-------------------------------------------^^^^^^^^^
$(function(){
  $("#topbar").load("b.html");
});
</script>

Note the </script><script> addition.

Marc B
  • 356,200
  • 43
  • 426
  • 500
-1

try this instead:

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.js"></script>
<script type="text/javascript">
$(function(){
    $.ajax({
      method: "GET",
      url: "b.html",
      dataType: 'html',
      data: { },
      success: function(data) {
          $("#topbar").html(data);
      }
    })      
});
</script>
</head>

<body>
<div id="topbar"></div>
</body>
</html>
Luc Laverdure
  • 1,398
  • 2
  • 19
  • 36