Axel Richter: iframe.document von "aussen" mit EventHandler versehen

Beitrag lesen

Hallo,

Ich rufe mittels <body onload="init();"> folgende Funktion auf:

function init()
{
for(i = 0; i < window.frames.length; i++)
{
  window.frames[i].document.onkeydown = function(){alert("@#&*^!")};
  alert(window.frames[i].document.onkeydown);
}
}

Wie gesagt, nur Firefox will nicht, Opera und IE spucken die alert-Box aus.

Weiss wer wieso?

Was steht in den IFrames drin? Wenn die IFrames nämlich Inhalte haben, kann ich das nicht nachvollziehen.

Beispiel:

  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  
        "http://www.w3.org/TR/html4/strict.dtd">  
<html>  
<head>  
<title>IFrames document.onkeydown</title>  
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">  
<style type="text/css">  
iframe { border:1px solid; }  
</style>  
<script type="text/javascript">  
<!--  
function init()  
{  
 for(i = 0; i < window.frames.length; i++)  
 {  
  window.frames[i].document.onkeydown = function(){alert(this.location.href)};  
  alert(window.frames[i].document.onkeydown);  
 }  
}  
//-->  
</script>  
</head>  
<body onload="init();">  
<iframe src="dummy"></iframe><!-- onkeydown funktioniert (location.href=dummy) //-->  
<iframe src=""></iframe><!-- onkeydown funktioniert (location.href=about:blank) //-->  
<iframe src="#"></iframe><!-- onkeydown funktioniert nicht; offensichtlich hat das Dokument hier keine Ausdehnung und kann nicht focusiert werden //-->  
</body>  
</html>  

viele Grüße

Axel