简体   繁体   中英

load html from external source

I want to include a snipped of HTML in multiple pages (each with a different development worflow & framework). In order to avoid having to update each page when that snipped changes, each page is loading a Javascript file that currently looks like this

var html = "lots of html with <style> and <script> tags";
var wrapper = document.createElement("div");
wrapper.innerHTML = html;
document.querySelector('body').appendChild(wrapper);

I'm looking for a better solution to inject HTML-content from another source using Javascript.

Try this:

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
        <script type="text/javascript">
           $(document).ready(function(){
               $('#selectedTarget').load('path/to/file.html');
           });
        </script>   
    </head>
    <body>
        <div id="selectedTarget">
            Existing content.
        </div>
    </body>
</html>

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