3
votes

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

3
Are you creating the IFrame tag in the script? Are you doing any other stuff in the DOM, like moving it around? Are you using a dialog box like Lightbox or one of its colleagues?Pekka
Please show the FULL CODE, and you may want to use XHR methods ( Ajax ) instead of an iframe.meder omuraliev
First of all thanks for your comments. @Pekka, The IFrame tag is being created in the HTML document as shown above and I am only using plain HTML (and a line of JavaCcript). @meder. That is a fully working sample (so it is a full code). Basically the source being set by JavaScript is executed twice by IE. XHR methods are out of the question in my scenario since the src will be a remote location and that is not supported by Ajax.Pierre Grima
strange. Can you try putting the javascript into the onload and see whether that changes anything?Pekka

3 Answers

11
votes

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.

0
votes

I also encountered this same problem, but in my case the cause was IE10. The src that I set dynamically to the IFRAME was opened twice. I resolved the issue by including this in the section :

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9;">
-1
votes

Put the script in the

<head> </head>

not in the

<body></body>

The reason to this is as follows, it will run the iframe once and draw this up, then because the script follows, it will run it again. Had same issue, dropped iframe, due to the demand from search engines want you not to use iframe.