2011-09-17 164 views
2

我在.aspx页面的头部添加了“test.js”文件。在test.js中我添加了一个脚本“document.body.setAttribute(”onload“,”testload()“);”document.body.setAttribute在IE-7中不起作用

这是在IE-8-9,Mozilla,Chrome和加载testLoad()函数中运行良好。

但它不工作在IE-7。

如何在IE-7中从test.js文件设置“body”的属性。

+0

本发明的设置onload的方式。我从来没有见过。 – mplungjan

回答

1

为什么不

window.onload = testload; 

window.onload = function() 
{ 
    //Some codes 
} 

注意body.onloadwindow.onload是不同的。如果要在所有资源加载后执行您的事件处理程序,请使用window.onload

+0

我用过相同的,它工作正常 – Shailesh

+0

window.onload = load; – Shailesh

+0

@Shai,当然。如果有效,您可以将我的答案标记为正确答案。谢谢 – dpp

0

IE7不支持obj.setAttribute('onload', doSomething);。您可以使用计时器处理IE。

var myiFrame = document.createElement("iframe"); 
myiFrame.setAttribute("id", "myiFrame"); 
myiFrame.setAttribute("src", "something.aspx"); 
myiFrame.setAttribute("class", "myclass"); 
myiFrame.setAttribute("frameBorder", "0"); //For IE 
myiFrame.setAttribute("hspace", "0"); 
//For all: 
myiFrame.setAttribute("onload", "testload();"); 
document.getElementById("myDiv").appendChild(myiFrame); 
//For IE: 
if (isIE = /*@[email protected]*/false) { 
    setTimeout(function() { testload() }, 500); 
} 

这就是它。如果您还想要加载事件监听器,则IE需要修复:

function testload() { 
//Add event listener for click so that 
//resize myiFrame in case any changes occur in size of content when user clicks 
    var content = document.getElementById("myiFrame").contentWindow.document.body; 
    if (content.addEventListener) { //For all 
     content.addEventListener('click', function() { 
      //find the height of the internal page 
      var the_height = content.scrollHeight; 

      //change the height of the iframe 
      document.getElementById("myiFrame").height = the_height + 10; 
     }, false); 
    } 
    else if (content.attachEvent) { //For IE 
     cerceveIci.attachEvent('onclick', function() { 
      //find the height of the internal page 
      var the_height = cerceveIci.scrollHeight; 

      //change the height of the iframe 
      document.getElementById("kk-iframe").height = the_height + 10; 
     }); 
    } 
}