2016-01-23 91 views
0

我想在多個頁面中包括一個HTML剪輯(每個頁面都有一個不同的開發工作流程&框架)。爲了避免時剪斷的變化,每個頁面加載,目前看起來像這樣從外部來源加載html

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

我正在尋找一個更好的解決方案使用以將來自其他來源的HTML內容的JavaScript文件更新每個頁面的JavaScript。

+0

這是不是一個真正的堆棧溢出的風格問題。你並沒有要求我們幫助解決代碼問題。您要求我們幫助改進您的解決方案。 您可能會從查看模板中受益。 – JBux

回答

1

試試這個:

<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>