代码中插入其他网页代码全版本

ASP/JSP版
被包括文件与父文件存在于同样文件夹中

<!-- #include file = "index.html" -->

被包括文件位于脚本虚拟文件夹中

<!-- #include virtual = "/index/index.html" --> 

include file 与include virtual的差别:#include file 包括文件的相对路径,#include virtual包括文件的虚拟路径,不论用#include file 还是 #include virtual,在路径中用“/”还是“/”或者二者交叉使用都不会影响编译效果,程序会顺利运行。

PHP版
实例代码1:

<?php echo readfile("index.html"); ?>

当然你也可以在简单一些

<?php readfile('index.html'); ?> 

PHP readfile() 函数相关说明文档

实例代码2:

<?php include 'index.html';?>

当然你也可以这样

<?php require 'index.html';?>

PHP include 和 require相关文档

实例代码3:

<?php echo file_get_contents("index.html");?>

PHP file_get_contents()相关文档

实例代码4:

<?php
   $basename = basename($_SERVER['PHP_SELF']); /* Returns The Current PHP File Name */
   if ($basename == 'index.php'){ ?>
      <iframe width="100%" height="1150px" src="index.html" frameborder="0" scrolling="yes"></iframe>
   <?php } ?>

PHP basename()相关文档

HTML版
一、jquery div+$(“#page1”).load(“b.html”)

参考代码:

<div id="index">
</div>
<script>
$(document).ready(function(){
    $('#index').load('index.html');
})
</script>

当然你也可以在简单一些

<div id="index"></div>
    <script>
          $("#index").load("index.html");
    </script>

二、iframe

参考代码:

<iframe src="index.html" frameborder="0" width="100%" height="430" allowtransparency="yes" style="overflow:hidden;margin: 0; border: none;"></iframe>

HTML <iframe 标签文档

三、object引入

参考代码:

<object style="border:0px" type="text/x-scriptlet" data="index.html" width=100% height=150>
</object>

HTML <object 标签文档

四、import引入

参考代码:

<link rel="import" href="index.html" id="index">
    <script>
        console.log(index.import.body.innerHTML);
    </script>

此代码测试无效,应该是我能力不足。
五、通过一个 include.js 控制引入文件。

1、将下方js文件代码保存成 include.js 文件引入;

2、在页面中通过 < include src=""> < include > 载入模板文件。

参考代码:

<script src="index.js"></script>
<include src="index.html"></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--){
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);
        }
        //执行文本中的额javascript  
        $this.executeScript(content);
        parent.removeChild(this);
        //替换元素 this.parentNode.replaceChild(includeNodes[1], this);  
    })
}
}
window.onload = function() {
    new Include39485748323().replaceIncludeElements();
}
})(window, document)

六、javascript方法
利用html/js互转功能将HTML代码转换成JS并插入代码

<script  language=“JavaScript”  type=“text/JavaScript” src="index.html" ></script>

当然你也可以在简单一些

<script  src="index.html" ></script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值