Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question A contentDocument.readyState Hack

    I needed to know if a remote Page was completely loaded in an iframe.
    If I try to use framename.contentDocument.readyState=='complete'
    I get an permissions error.
    However if a create a local Page such as:

    -----------------------------------------------
    <html>
    <body>
    <iframe src="http//:somewhere.com" > </iframe>
    </body>
    </html>
    ---------------------------------------
    I can check whether the local Page, hence the remote Page I was interested in is loaded.

    Will this hack go away or does it conform to the standard?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    an iframe should fire an onload event when it loads a new resource, the way images do when you change their source.

    No need to read the content.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your quick reply. Indeed it does fire an event and it may well be that I am going about things the wrong way but I am trying to synchronize the loading of two div(s).

    In more detail, my code, much abbreviated, is
    -----------------------------------------------------------
    function setURL(aURL){
    document.getElementById("theiframe").src = aURL;
    //I need to wait until the aURL is completely loaded.
    return;
    }

    -----------------------------------------------------
    I understand that javascript does not have the Threading I would normally use.

  • #4
    mjy
    mjy is offline
    New Coder
    Join Date
    Jan 2012
    Location
    United States
    Posts
    28
    Thanks
    0
    Thanked 6 Times in 6 Posts
    You can't access the Document object for a page from a different domain because of the same-origin policy. Regarding Jerrold's hack, I'm not sure that an onload event for the local page guarantees that the page in an embedded iframe is also fully loaded -- an embedded iframe might be loaded asynchronously.

    mrhoo's solution is a good one. Even though the onload event is nonstandard for an iframe, it's universally supported by modern browsers. Just make sure that you register the event handler immediately after creating the iframe (not in an onload handler) so that you don't miss the event. The following code works:
    Code:
    <body>
    <iframe src="http://example.com" id="myFrame"> </iframe>
    <script>
       document.getElementById ("myFrame").onload = function ()
          {
          alert ("iframe loaded");
          }
    </script>
    </body>

  • Users who have thanked mjy for this post:

    Jerrold (01-16-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks mjy!

    It does, indeed work!

    I did not know how to register the handler when I had first tried mrhoo's approach.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •