html代碼
<div ></div>
javascript代碼
function clientSideInclude(id, url) { var req = false; // Safari, Firefox, 及其他非微軟瀏覽器 if (window.XMLHttpRequest) { try { req = new XMLHttpRequest(); } catch (e) { req = false; } } else if (window.ActiveXObject) { // For Internet Explorer on Windows try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { req = false; } } } var element = document.getElementById(id); if (!element) { alert("函數(shù)clientSideInclude無法找到id " + id + "。" + "你的網(wǎng)頁中必須有一個含有這個id的div 或 span 標(biāo)簽。"); return; } if (req) { // 同步請求,等待收到全部內(nèi)容 req.open('GET', url, false); req.send(null); if (req.status == 404) { clientSideInclude(id, 'error.html') } else { element.innerHTML = req.responseText; } } else { element.innerHTML = "對不起,你的瀏覽器不支持" + "XMLHTTPRequest 對象。這個網(wǎng)頁的顯示要求" + "Internet Explorer 5 以上版本, " + "或 Firefox 或 Safari 瀏覽器,也可能會有其他可兼容的瀏覽器存在。"; }}clientSideInclude(includeHtml, "http://XXXXX.html");//頁面中的一個div的id為includeHtml
用法很簡單,代碼里已經(jīng)注釋了,這里在簡單描述下,給那些不看注釋的人看吧
js代碼在當(dāng)前頁面加載或者做成js文件加載進(jìn)來,然后遠(yuǎn)端的代碼會自動寫入到當(dāng)前頁面里id號為 includehtml的DIV里面
|
新聞熱點(diǎn)
疑難解答
圖片精選