Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:59:34+00:00 2026-06-01T17:59:34+00:00

If I run the following code, the body and head tags are removed. <iframe

  • 0

If I run the following code, the body and head tags are removed.

<iframe src='blank.htm'>
    <html>
    <head>
        <style type="text/css">

        </style>
    </head>
    <body>
        test
    </body>
    </html>
</iframe>
<script>
    $('iframe').load(function () {
        var contents = $('iframe').text();
        $('iframe').contents().find('html').html(contents);
    });
</script>

If I then remove the style tags, everything shows up correctly. Why is this and how can I get it to stop removing the head and body tags? I want the contents to be populated as is, without any manipulation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:59:35+00:00Added an answer on June 1, 2026 at 5:59 pm

    Here are steps you can follow to get an idea of how to copy the iframe content to a string, modify it, and replace it in the iframe. These steps are meant to be performed in your Chrome Debugger and are for educational/demonstration purposes only. Once you understand the process, you can then attempt to replicate in your code on your website.

    Run each function one at a time in Chrome Debugger:

    // test iframe I loaded in a sandbox also at http://example.com
    $('body div:first').before('<iframe src="http://example.com/company"></iframe>');
    
    // retrieved the body of the iframe - I cloned it so I could perform operations
      // on the copy without bothering the iframe content.
    iBody = $('iframe:first').contents().find('body').clone();
    
    // got the head
    iHead = $('iframe:first').contents().find('head').html();
    
    // if there is script in the body, it really messes things up. So in my tests, 
      // I had to remove it before adding it back to the iframe.
    iBody.find("script").remove();
    
    // verify there are no script elements
    ibody.html(); 
    
    // replace the iframe head
    $('iframe:first').contents().find('head').html(iHead);
    
    // replace the iframe body first with a placeholder. The body is the sensitive 
      // part. If you destroy the DOM, it can make the entire page, including 
        // the parent, turn completely white.
    $('iframe:first').contents().find('body').html('<div></div>');
    
    // ** inserted something in the copy to prove we modified it!!
    iBody.append("<div style='position:absolute;z-index:5000; color:red; " + 
        "background-color:white;'>JAMES WAS HERE</div>");
    
    // now replace the body
    $('iframe:first').contents().find('body').html(iBody);
    

    In the bottom of the website, inside the iframe, I saw my message in red on a white background. I could also see it in the source:

    $('iframe:first').contents().find('body').html();
    

    I performed these actions on an anonymous website, using Chrome’s Debugger console to run the commands. The first command creates an iframe in the home page with the site’s company page in the iframe. The remaining commands get the head and body of that iframe, clone the body, add some stuff, like a “JAMES WAS HERE” message, to the body clone, and then replace the iframe body with that copy.

    I also verified the same results by loading http://getsatisfaction.com in my browser and loading http://getsatisfaction.com/explore/product-innovation in the iframe. By repeating the above steps, I saw my message, “JAMES WAS HERE”.

    **The most severe problem I ran into was not being able to leave the JavaScript or CSS style tags in the copy. The solution for styling is to make all style modifications before adding the body back to the iframe. **

    For some reason, the browser attempted to run the JavaScript and threw errors. The context of those errors made me think that the JavaScript was being run in the toplevel context instead of the iframe context! This may be a showstopper for you. You may need to come up with another plan for showing previews in a popup window or inline. Also, this may behave differently on different websites with different JavaScript. It also may behave strangely in IE.

    In short, I don’t actually see this solution being something that would support a production website, since the JavaScript must be removed in the iframe; however, depending on your needs, it may be possible to make this work in production if you don’t need the iframe DOM to use any JavaScript.

    Nevertheless, this was an interesting problem to explore!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run the following code echo <html> <head> <style type=text/css> p {color:
When I'm trying to run the following code in IE:- <html> <head> <script language=javascript>
I want to run following html/JS code in PhoneGap, but the alert dialog is
I have written the following html code <!DOCTYPE html> <head> <title>Presentation on HTML5</title> <center>Contents</center>
I'm trying to run this code using tomcat in eclipse <%@ page language=java contentType=text/html;
I have the following code: //set $message and run database query $message .= '<body
I have the following code: <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/TR/html4/strict.dtd> <html> <head>
I have following sample code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.1//EN http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd> <html> <head>
I have the following snippet of the code: <html> <head> <title>Example of Very First
when I trying to run following code. var result = from c in db.brand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.