SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)

    Put IFRAME source code into textarea

    I would you grab the source code from an IFRAME and display it in a textarea?

    I'm trying to create a sort of preview page that both shows a rendered page, then shows the source code next to it.

    [edit]: I'm thinking that using OnLoad JS event in the HTML body tag would allow this to work. The page loads, the OnLoad event calls a JS function that pulls the innerHTML from the IFRAME and inserts it into the textarea.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The innerHTML from the iframe is just gonna be the src and other attributes
    you set explicitly. You want the iframeElement's

    .contentWindow.document.documentElement.innerHTML

  3. #3
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    ok, so how do you then put that in a textarea?

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    textareaReference.value= iframeReference.contentWindow.document.documentElement.innerHTML

  5. #5
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    Code:
    document.getElementById('ta_value').value = document.getElementById('iframe_value').contentWindow.document.body.innerHTML;
    That seemed to work, albeit not perfectly. It just grabs the body code, and not anything outside of it. Also, the code that does get pulled is rewritten. For example, table tags get turned into tbody tags.


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
  •