常常需要在页面中嵌入iframe地址,但通常加载iframe会影响到页面打开速度。js可以动态的嵌入iframe,如果需要也可以在某事件触发时进行相关的操作,从而提高页面响应速度。
js动态加载iframe代码如下:
function LoadIframe(){
var newIframeObject = document.createElement( " IFRAME " );
newIframeObject.src = " http://v.788111.com/v2/video/vodtest.aspx " ;
newIframeObject.scrolling = " no " ;
newIframeObject.frameBorder = 0 ;
newIframeObject.width = 288 ;
newIframeObject.height = 65 ;
$( " feature_info " ).appendChild(newIframeObject);
}
function $(s) { return document.getElementById(s); };
var newIframeObject = document.createElement( " IFRAME " );
newIframeObject.src = " http://v.788111.com/v2/video/vodtest.aspx " ;
newIframeObject.scrolling = " no " ;
newIframeObject.frameBorder = 0 ;
newIframeObject.width = 288 ;
newIframeObject.height = 65 ;
$( " feature_info " ).appendChild(newIframeObject);
}
function $(s) { return document.getElementById(s); };
将该函数写在自己需要的事件中,便可实现动态加载的效果。