load html from external source

359 views Asked by At

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.

1

There are 1 answers

0
kzhao14 On

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>