1、最应该先想到的应该是iframe标签吧。但是有时候用iframe时,会出现在iframe中的子页面中的页面跳转的链接,显示的时候会只显示在iframe部分。所以想了下一个方法
2、用jquery提供的load()方法加载另一个页面,$('要显示在的标签').load('另一个页面的路径');温馨提示:会出现安全问题。解决方法一会说
3、用<object style="border:0px" type="text/x-scriptlet" data="目标html页面l" width=100% height=100%></object>标签也能实现
4、将下面代码存为include.js文件中,然后在页面上用<include src="目标页面"></include>。这样也会出现跟用jquery中的load方法一样的问题
(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--){ 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++; } 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++) { parent.insertBefore(includeNodes[0], this); } $this.executeScript(content); parent.removeChild(this); }) } } window.onload = function() { new Include39485748323().replaceIncludeElements(); } })(window, document)
问题:
这个问题就是因为直接打开的话是用ftp协议访问的,这两种方法不支持,所以要改成http协议。
解决方法:在cmd中启动一个服务,然后访问就没问题了。