SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast BurakUeda's Avatar
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    iframe problem: Can read contents with innerHTML, but cannot insert.

    I can read the contents of an iframe:
    HTML Code:
    var iframe = document.getElementById("message");
    var iframeContents = iframe.contentDocument.body.innerHTML;
    
    var txtarea = document.getElementById('message2');
    txtarea.value = iframeContents;
    This will read the contents of an iframe, and write it to a textarea.
    iframe is editable, and content is entered by the user. Still, this works.

    But, below code is not working. Trying to put some content to an iframe, with the same innerHTML method above. It returns no errors or warnings. Even last alert() displays "<div>lorem ipsum</div>", but iframe is empty, it displays nothing..
    HTML Code:
    var textContent = "lorem ipsum";
    var iframe2 = document.getElementById('message');
    iframe2.contentDocument.body.innerHTML = '<div>'+textContent+'</div>';
    	
    alert(iframe.contentDocument.body.innerHTML);	
    Any help greatly appreciated.
    H u m o
    Uncensored Forums for Intelligent People

  2. #2
    SitePoint Enthusiast BurakUeda's Avatar
    Join Date
    Apr 2005
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry guys, I found the problem.
    It was the speed of the executing the script, again....
    It proceeds and executes next line, without receiving the results of the current line.

    JavaScript seems to have this problem if you create elements on the fly with createElement function. I put the above script into a function, and set a timeout: setTimeout(delayInsert,250) and problem solved.
    H u m o
    Uncensored Forums for Intelligent People

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    with(iFrame) {
               onreadystatechange = function() {
                                               this.contentDocument.body.innerHTML = "blabla";
               }                                          
    }
    this works in ie but i cant find out the way for firefox without using timeout.
    any suggestion wellcome!!!
    thanx in advance
    regards

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    NJ
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi reminder,

    I believe that the onreadystatechange and others like it such as oncontentready are exclusive to ie. Check out this link:

    http://msdn2.microsoft.com/en-us/library/aa360250.aspx
    Last edited by hddd12345678910; Dec 9, 2007 at 06:57. Reason: Wrong Salutation


Bookmarks

Posting Permissions

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