webForumDet fria alternativet

ifram onload IE problem

2 svar · 368 visningar · startad av niels

nielsMedlem sedan juli 2000215 inlägg
#1

Look at the following code:

<html>
<head>

</head>
<body>

<iframe id="olaf" name="olaf" onload="alert('no');"></iframe><br>
<input type="submit" onclick="document.getElementById('olaf').src = 'phpinfo.php'" value="Test">

</body>

<script language="javascript">
var obj = document.getElementById('olaf');
obj.onload = function() { alert('yes'); }
</script>

</html>

The "obj.onload=" contruct should override the first onload statement. In Firefox it works fine and when I click the button a "yes" message is shown. But in Internet Explorer the "no" message is shown. My problem is that I need to set the function (anonmymous) that should be called by the brower when iframe.onload and I can't get this to workin in IE. Can anyone help?

Thanks!
/Niels

yohpopsMedlem sedan feb. 20011 102 inlägg
#2

Why do you need to use both "onloads"?
When I use your code in IE I get both messages.

nitro2k01Medlem sedan aug. 20037 630 inlägg
#3

Why english?
The problem with here is that IE adds the event listener defined inline internally instead of setting as an attribute for the DOM object. Unfortunately there's no way around this problem when using an inline onload handler. You could either store a variable, or use javascript to attach both handlers. I'd recommened the latter.

<html>
<head>

</head>
<body>

<iframe id="olaf" name="olaf" ></iframe>
<script type="text/javascript">
document.getElementById('olaf').onload = function() { alert('no'); }
</script>

<br />

<input type="submit" onclick="document.getElementById('olaf').src = 'phpinfo.php'" value="Test" />

</body>

<script type="text/javascript">
// Add a bit of randomness to show that the concept works.
if (Math.random()>.5)
	document.getElementById('olaf').onload = function() { alert('yes'); }

</script>

</html>
Genererad på 377 ms · cache AV · v20260730165559-full.f96bc7eb