Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iframe src set dynamically through JavaScript is being executed twice on Internet Explorer

I am encountering a very annoying problem with IE. Basically I need to set the source of an IFrame using JavaScript, however the source document is being executed twice not once during each call.

The simplified HTML code is pasted below (I simplified it so that readers can understand it quickly. The source is being set through JavaScript since it will contain dynamic content):

<html>
<head>
<title>Iframe test</title>
</head>
<body>

<iframe id="testIframe" scrolling="no" frameborder="0" src="" width="800" height="600"></iframe>

<script language="JavaScript">
    document.getElementById("testIframe").src = "http://localhost/test.php";
</script>

</body>
</html>

In this example, test.php inserts a record inside a local database once called. Whenever the page above is called using IE, two rows are being inserted on most occasions (sometimes only 1 row is inserted but this is not the norm). I tested the same script on Chrome and Opera and it works correctly on them so this must be an IE issue.

If is set the src directly inside the iframe tag IE starts behaving correctly, however I need to be able to build the URL using javascript.

Has anyone encountered this issue? and does anyone know of a solution/workaround?

Thanks and Regards Pierre

like image 684
Pierre Grima Avatar asked Jan 02 '26 09:01

Pierre Grima


1 Answers

I managed to solve the issue. Basically I am now using

window.frames['testIframe'].document.location.href

instead of

document.getElementById("testIframe").src

and the source document is being hit only once.

like image 72
Pierre Grima Avatar answered Jan 03 '26 23:01

Pierre Grima



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!