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 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Iframe specific content

    Is it possible to take a specific content into iframe?
    I mean i have an iframe on page 1.
    I want to take into this iframe a div (let's say id="myDiv") from page 2,but only "myDiv" should appear in the iframe.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    If both documents are in the same domain:
    Code:
    function copyIt() {
    var iframe = document.getElementsByTagName("iframe")[0]; // first iframe
    var p = document.getElementById("myDiv"); 
    var p_copy = p.cloneNode(true);
    iframe.contentDocument.body.innerHTML = '';
    iframe.contentDocument.body.appendChild(p_copy);
    }

  • #3
    New Coder
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry,i forgot to say the two pages are on different domain.

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    Load your own blank document into the iframe then use the script

  • #5
    New Coder
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Sorry,i don't get it.I understand your function,but how can i pass the page where my div derives from,to javascript?

  • #6
    Regular Coder
    Join Date
    Jun 2004
    Posts
    495
    Thanks
    0
    Thanked 82 Times in 80 Posts
    Code:
    function copyIt() {
    var iframe = document.getElementsByTagName("iframe")[0]; // first iframe
    iframe.src='blank.html'; // load empty document
    var p = document.getElementById("myDiv"); // the element you want to copy
    var p_copy = p.cloneNode(true); // copy the element
    iframe.contentDocument.body.innerHTML = ''; // clear the iframe document
    iframe.contentDocument.body.appendChild(p_copy); // add the element to the iframe document
    }


  •  

    Posting Permissions

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