hi guys onscroll event不适用于跨域iframe。我知道为了这个工作,iframe应该是相同的域。
下面是我试过的代码。
var oE1;
function createIframe(){
var i = document.createElement("iframe");
i.src = "http://ads1.msn.com/ads/demos/Bing/09.17.10_HomePage_US_300x600_Bing_Permanent.htm";
// i.src='inner.html';
i.scrolling = "auto";
i.frameborder = "0";
i.width = "600px";
i.height = "600px";
i.id='frm';
document.getElementById("iframe").appendChild(i);
oE1 = getFrameTargetElement(document.getElementById("frm"));
if(oE1){
oE1.onscroll=function(){
console.log("iframe scroll");
};
}else{
console.log("iframe not found");
}
}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", createIframe, false);
else if (window.attachEvent)
window.attachEvent("onload", createIframe);
else window.onload = createIframe;
function getFrameTargetElement(oI){
var lF = oI.contentWindow;
if(window.pageYOffset==undefined)
{
//IE
lF= (lF.document.documentElement) ? lF.document.documentElement : lF=document.body;
}
//- return computed value
return lF;
}
想知道如何继续跨域iframe,并且应该可以在大多数浏览器中使用。