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 7715125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:18:26+00:00 2026-06-01T02:18:26+00:00

Simple question which I can’t seem to find an answer of: I have two

  • 0

Simple question which I can’t seem to find an answer of:
I have two iframes on a page and I’d like to copy the content of the first one to the second.
But I can’t do it by just copying the url of the first iframe to the second since the containing page is a dynamic one.

This code does do it, but a lot of the page-formatting seems to get lost. And I don’t know if it’s cross-browser either.

iframe2.contentWindow.document.write(iframe1.contentWindow.document.body.innerHTML);

Can this be done?

  • 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-01T02:18:27+00:00Added an answer on June 1, 2026 at 2:18 am

    Native JavaScript Solution As Asked For:

    First, to make things simple I created 2 object literals:

    var iframe1 = {
    
        doc     : undefined,
        head    : undefined,
        body    : undefined
    
    };
    
    var iframe2 = {
    
        doc     : undefined,
        head    : undefined,
        body    : undefined
    
    };
    

    Next, I put everything under iframe1’s window.onload handler to make sure it was loaded fully:

    document.getElementById("iframe1").contentWindow.onload = function() { 
    

    Then I assigned all of the object literal properties:

        iframe1.doc = document.getElementById("iframe1").contentWindow.document;
        iframe1.head = iframe1.doc.getElementsByTagName("head")[0];
        iframe1.body = iframe1.doc.getElementsByTagName("body")[0];
    
        iframe2.doc = document.getElementById("iframe2").contentWindow.document;
        iframe2.head = iframe2.doc.getElementsByTagName("head")[0];
        iframe2.body = iframe2.doc.getElementsByTagName("body")[0];
    

    Next, I needed to create a couple functions removeNodes() and appendNodes() so that I could re-factor some code that is used for both <head> and <body> routines.

        function removeNodes(node) {
    
            while (node.firstChild) { 
    
                console.log("removing: " + node.firstChild.nodeName);
                node.removeChild(node.firstChild); 
    
            } 
        }
    

    and:

        function appendNodes(iframe1Node, iframe2Node) {
    
            var child = iframe1Node.firstChild;
            while (child) { 
    
            if (child.nodeType === Node.ELEMENT_NODE) { 
    
                console.log("appending: " + child.nodeName);
    
                if (child.nodeName === "SCRIPT") {
    
                    // We need to create the script element the old-fashioned way
                    // and append it to the DOM for IE to recognize it.
    
                    var script = iframe2.doc.createElement("script");
                    script.type = child.type;
                    script.src = child.src;
    
                    iframe2Node.appendChild(script);
    
                } else { 
    
                    // Otherwise, we append it the regular way. Note that we are
                    // using importNode() here. This is the proper way to create                        
                    // a copy of a node from an external document that can be 
                    // inserted into the current document. For more, visit MDN:
                    // https://developer.mozilla.org/en/DOM/document.importNode
    
                    iframe2Node.appendChild(iframe2.doc.importNode(child, true)); 
                } 
            }
    
            child = child.nextSibling;
        }
    

    With those functions created, now all we have to do is make our calls:

        console.log("begin removing <head> nodes of iframe2");
        removeNodes(iframe2.head);
    
        console.log("begin removing <body> nodes of iframe2");  
        removeNodes(iframe2.body);
    
        console.log("begin appending <head> nodes of iframe1 to iframe2");
        appendNodes(iframe1.head, iframe2.head);
    
        console.log("begin appending <body> nodes of iframe1 to iframe2");
        appendNodes(iframe1.body, iframe2.body);
    

    … and finally, we close off the window.onload function:

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

Sidebar

Related Questions

got a very simple question which I can't seem to find any answer for.
I have a very simple question for which I can't seem to find an
Simple question which I can't seem to find the answer on the web. How
I have a simple security question which I just can't find the answer to.
Simple question to which I can't find any nice answer by myself: Let's say
A fairly simple question for which I have a guess, but I can't find
Simple question which I can't find the answer to: How can I use JavaScript
Simple question, can't seem to find an answer on Google. I'm normalizing a database
Simple question but I can't seem to find the answer - can someone tell
I have a simple question and I hope I can find answer here! Can

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.