html頁面引入另一個html頁面


我們在使用代碼編程一個網站的時候,通常情況下頭部和尾部是相同的,如果一個網站的每個頁面都把這些代碼寫一遍,不僅浪費時間,還顯得重復代碼很多,所以此時把重復的頁面單獨摘出來,在用到的時候從外部直接引進去,就能節省很多時間,減少很多代碼。

在這里,有好幾種引入html文件的方式,不過每種都是有利有弊,需要根據需要自行選擇
有大佬反饋解決了有些瀏覽器本地實現不了的問題:放到服務器上面!(感謝!!!)

注意:引入后主頁面的Css樣式不適用於被引入頁面,比如在主頁面設置

<style type="text/css"> *{ margin: 0; padding: 0; } </style> 

只在本頁面生效,引入文件整體適用,但引入文件內容不適用。


  • import引入(<head>中引入文件,<script>中加載內容)
href鏈接引入的html文件,id可以看做頁面引導,在script中用到
<head> <meta charset="utf-8" /> <title>主頁面</title> <!--import引入--> <link rel="import" href="top.html" id="page1"/> <link rel="import" href="fotter.html" id="page2"/> </head> 
<!--注意順序--> <!--import在頭部引入,里面是啥就是啥--> <script type="text/javascript"> document.write(page1.import.body.innerHTML); </script> 你好呀! <!--本頁面寫入內容--> <script type="text/javascript"> document.write(page2.import.body.innerHTML); </script> 

最后的運行效果:

 
import引入運行效果

為了客觀反應不同方式的引入差距,下面是瀏覽器代碼界面:
 
控制台檢查

由上圖可以看出, import引入除開script標簽,在其他html body中寫入什么就引入什么,完全的內容格式.

 


  • js引入
<!--注意順序--> <!--使用js引入,引入整個文檔,但是沒有html和body,相當於body里面的數據--> <div class="top"></div> <div class="center"> <p>你好,我在中間!</p> </div> <div class="footer"></div> <script src="js/jq/jquery-3.2.1.min.js"></script> <script type="text/javascript"> //在js中引入 $(document).ready(function () { $('.top').load('top.html'); $('.footer').load('fotter.html'); }); </script> 

使用js引入,相當於把引入的html中head和body標簽中的數據拖出來,在外面包了一個你自己寫的標簽,比如說上面代碼中的<div class="top"></div>

運行結果同import相同,這里不再展示

注意:是head和body標簽中的數據,不帶標簽,下圖是瀏覽器顯示代碼
 
js引入

  • include 引入(涉及到一個從網上扒的封裝函數,下面有)(head和body標簽中的數據直接引入)
<body> <!--include引入,順序很重要--> <script src="js/include.js"></script> <include src="top.html"></include> <include src="center.html"></include> <div id=""> <p>你沒有看錯,我在這!</p> </div> <include src="fotter.html"></include> </body> 

瀏覽器代碼分析

 
include引入

 

include.js壓縮代碼:

(function(window,document,undefined){var Include39485748323=function(){};Include39485748323.prototype={forEach:function(array,callback){var size=array.length;for(var i=size-1;i>=0;i-=1){callback.apply(array[i],[i])}},getFilePath:function(){var curWwwPath=window.document.location.href;var pathName=window.document.location.pathname;var localhostPaht=curWwwPath.substring(0,curWwwPath.indexOf(pathName));var projectName=pathName.substring(0,pathName.substr(1).lastIndexOf('/')+1);return localhostPaht+projectName},getFileContent:function(url){var ie=navigator.userAgent.indexOf('MSIE')>0;var o=ie?new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();o.open('get',url,false);o.send(null);return o.responseText},parseNode:function(content){var objE=document.createElement("div");objE.innerHTML=content;return objE.childNodes},executeScript:function(content){var mac=/<script>([\s\S]*?)<\/script>/g;var r="";while(r=mac.exec(content)){eval(r[1])}},getHtml:function(content){var mac=/<script>([\s\S]*?)<\/script>/g;content.replace(mac,"");return content},getPrevCount:function(src){var mac=/\.\.\//g;var count=0;while(mac.exec(src)){count+=1}return count},getRequestUrl:function(filePath,src){if(/http:\/\//g.test(src)){return src}var prevCount=this.getPrevCount(src);while(prevCount--){filePath=filePath.substring(0,filePath.substr(1).lastIndexOf('/')+1)}return filePath+"/"+src.replace(/\.\.\//g,"")},replaceIncludeElements:function(){var $this=this;var filePath=$this.getFilePath();var includeTals=document.getElementsByTagName("include");this.forEach(includeTals,function(){var src=this.getAttribute("src");var content=$this.getFileContent($this.getRequestUrl(filePath,src));var parent=this.parentNode;var includeNodes=$this.parseNode($this.getHtml(content));var size=includeNodes.length;for(var i=0;i<size;i+=1){parent.insertBefore(includeNodes[0],this)}$this.executeScript(content);parent.removeChild(this);})}};window.onload=function(){new Include39485748323().replaceIncludeElements()}})(window,document); 
  • object引入和iframe引入(帶有滾動條,視情況使用)
<!--object引入,相當於把整個頁面拉過來(在一個html中嵌套另一個html),包括title,meta,body,html等--> <!--此處的高是嵌套進去的整個html的高,不包括邊框,padding等--> <object style="border:1px solid red" type="text/x-scriptlet" data="top.html" width="100%" height="200px"></object> <!--iframe引入,同object方式一樣,頁面整個嵌套,默認高度為150,frameborder設置為1時邊框寬度為2--> <iframe marginwidth=0 marginheight=0 width="100%" height=200 src="top.html" frameborder="no" <!--scrolling="no"-->></iframe> 

兩中引入方式比較:

  • 相同點:
    1. 默認高度為150
    2. 引入后本頁面html嵌套引入頁面html,整個引入
  • 不同點:
    1. iframe引入使用scrolling="no"可以不讓頁面進行滾動,取消右側滾動條
    2. iframe中 frameborder="no"可以修改為0或1,這里不是指寬度,可以理解為布爾型,當設為1時border寬度為2
      以iframe為例展示引入top.html瀏覽器代碼
       
      iframe引入


作者:東冥羽
鏈接: https://www.jianshu.com/p/c4f18bea8cab
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM