自己开发的两个网站,现在的需求是在a网站首页中的某个div内嵌到b网站的某个页面,先说明:1,不能在a网站首页写死使用iframe内嵌。就是查看源码的时候不能看到这个iframe及iframe里面的内容,只能看到a网站首页的源代码。
2,可以考虑使用js注入iframe内嵌。 求教思路!
a网站需要在引入的地方导入b.js,b.js内容如下
document.write('<iframe src="http://www.bbb.com/xxxx.html"></iframe>')
a网站首页
......其他内容
<script src="b.js"></script>
......其他内容
<body body style="margin:0;padding:0;overflow:hidden" scroll="no">
<div>
<iframe frameborder="0" src="http://localhost/File/home/index?url=http://localhost:2000/StuSearch.html" name="search_frmleft" scrolling="no" noresize width="72.8%" height="100%" align="left" overflow="auto"></iframe>
<iframe frameborder="0" src="../index/info.html" name="search_frmright" scrolling="no" noresize width="26.8%" height="100%" align="right"></iframe>
</div>
</body>
<div id="divID"></div>
<script type="text/javascript">
$(function(){
var ifr = $("<iframe>").prop("src","https://www.baidu.com/");
$("#divID").append(ifr);
});
</script>
除了iframe的方法外,也可以用jquery的ajax实现,示例代码如下:
1.html:
<html>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
$.ajax({
url:"2.html",
async:false,
type:"get",
dataType:"html",
success:function(msg){
$("#2").html(msg)
}
})
</script>
<div id="1">网页1内容</div>
<div id="2"></div>
</html>
2.html:
<p>网页2内容</p>
如果两个网站的服务器在一起建议使用反向代理,反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
js跨域很多时候是被禁止的,有安全隐患,银行之类的都是直接堵死的。