简体   繁体   中英

Why is JQuery LoadHTML function not adding my html to my page?

I am trying to load html content from another file to my main page using JQuery. I am doing this because I have a navigation bar and footer that will be constant throughout all the pages. I wrote them independently in there own files first and tested each of them so I know they work when they are statically placed into the html file. However, when I try to load them using JQuery loadHTML function I am having issues getting them on the page.

Here is my index.html file for reference:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-  awesome/4.7.0/css/font-awesome.min.css"> 
    <link href="NavBarStyle.css" rel="stylesheet">  
    <link href="FooterStyle.css" rel="stylesheet">
</head>
<body>
    <header></header>

    <div><h1>Content Goes Here</h1></div>

    <footer></footer>

    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src ="NavBar.js"></script>
    <script>
        $(document).ready(function () {
            $("header").load("NavBar.html", function(){
                $("header").enhanceWithin(); 
            });
            $("footer").load("Footer.html", function(){
                $("footer").enhanceWithin();
            });
        });
    </script>
</body>
</html>

My NavBar.html

<div class = "navbar">
    <div class="FB">
        <iframe  id="share" class = "facebook-bt" src="https://www.facebook.com/plugins/share_button.php?href=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&layout=button_count&size=small&width=96&height=20&appId" width="96" height="20" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"></iframe>
        <iframe id="like" class = "facebook-bt"src="https://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&width=450&layout=standard&action=like&size=small&share=true&height=35&appId" width="450" height="35" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; picture-in-picture; web-share"></iframe>
    </div>
    <div class="navbar-items">
        <div class="navbar-links">
            <div id = "big" class="dropdown">
                <button class="dropbtn">ABOUT
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">MEET US</a>
                    <a href="#">TESTIMONIALS</a>
                    <a href="#">OFFICE TOUR</a>
                    <a href="#">HOURS</a>
                </div>
            </div> 
            <div id = "big" class="dropdown">
                <button class="dropbtn">PATIENTS
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">WHAT TO EXPECT</a>
                    <a href="#">PAPERWORK</a>
                    <a href="#">FAQ's</a>
                    <a href="#">HEALTH TIPS</a>
                    <a href="#">BLOG</a>
                </div>
            </div> 
            <div id = "big" class="dropdown">
                <button class="dropbtn">SERVICES
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">CHIROPRACTIC</a>
                    <a href="#">NUTRITION</a>
                    <a href="#">MASSAGE</a>
                    <a href="#">OTHER</a>
                </div>
            </div> 
            <div id = "big" class="dropdown">
                <button class="dropbtn">REVIEWS
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">REVIEW US</a>
                    <a href="#">TESTIMONIALS</a>
                </div>
            </div> 
            <div id = "big" class="dropdown">
                <button class="dropbtn">CONTACT
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">CONTACT INFO</a>
                    <a href="#">LOCATION</a>
                </div>
            </div> 
            <div class="burger">
                <div></div>
                <div></div>
                <div></div>
            </div>
        </div>
    </div>
</div>
<div class="small-Nav">
    <div class="navbar-links">
            <div id = "small" class="dropdown">
                <button class="dropbtn">ABOUT
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content-small">
                    <a href="#">MEET US</a>
                    <a href="#">TESTIMONIALS</a>
                    <a href="#">OFFICE TOUR</a>
                    <a href="#">HOURS</a>
                </div>
            <div id = "small" class="dropdown">
                <button class="dropbtn">PATIENTS
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content-small">
                    <a href="#">WHAT TO EXPECT</a>
                    <a href="#">PAPERWORK</a>
                    <a href="#">FAQ's</a>
                    <a href="#">HEALTH TIPS</a>
                    <a href="#">BLOG</a>
                </div>
            </div> 
            <div id = "small" class="dropdown-small">
                <button class="dropbtn">SERVICES
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content">
                    <a href="#">CHIROPRACTIC</a>
                    <a href="#">NUTRITION</a>
                    <a href="#">MASSAGE</a>
                    <a href="#">OTHER</a>
                </div>
            </div> 
            <div id = "small" class="dropdown">
                <button class="dropbtn">REVIEWS
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content-small">
                    <a href="#">REVIEW US</a>
                    <a href="#">TESTIMONIALS</a>
                </div>
            </div> 
            <div id = "small" class="dropdown">
                <button class="dropbtn">CONTACT
                    <i class="fa fa-caret-down"></i>
                </button>
                <div class="dropdown-content-small">
                    <a href="#">CONTACT INFO</a>
                    <a href="#">LOCATION</a>
                </div>
            </div> 
        </div> 
    </div>
</div>
<a href  = "#">
    <div class = "logo-circle">
        <div class="logo"></div>
    </div>
</a>

My footer is set up the same way as the NavBar.html file only the content is slightly different.
What am I doing wrong that is causing the files to not load up?

You don't need enhanceWithin call, so your code should look like this:

$(document).ready(function () {
    $("header").load("NavBar.html");
    $("footer").load("Footer.html");
});

Check load method documentation here for details.


I also suggest checking paths to files and their names (maybe you are getting 404 errors)

Try to debug your issue through dev. console by pressing F12 . You should see several errors. Something like Failed to load resource: net::ERR_FAILED and cors .

Are you running a webserver or index.html directly from the folder? The problem is .load doesn't run directly please read up on load from jquery as the answer above mentions.

You will need to run index.html through your webserver and you will see the file load the requested file.

I get this as message: header

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <header></header>

    <div><h1>Content Goes Here</h1></div>

    <footer></footer>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $("header").load("NavBar.html", function(){
               //  $("header").enhanceWithin(); // Ignoring this line since question is not asking to fix the errors regarding enhance. I'll leave that upto you.
             });
        });
    </script>
</body>
</html>

You can simply use without JQuery as well

<header>
<include src="My NavBar.html"></include>
</header>

<footer>
<include src="My Footer.html"></include>
</footer>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM