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

31,866

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.

Share:
31,866
Admin
Author by

Admin

Updated on October 13, 2020

Comments

  • Admin
    Admin over 3 years

    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