webForumDet fria alternativet

ifram onload IE problem

JavaScript

2 svar · 368 visningar · startad av niels

Medlem sedan juli 2000212 inlägg
Frågan#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

Medlem sedan feb. 20011 198 inlägg
#2

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

Medlem sedan aug. 20039 340 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>
256 ms totalt · 4 externa anrop · v20260731065814-full.1dc6f849
123 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
123 ms — ändringar (db)