简体   繁体   中英

Copy Content from Iframe to div

I would like to copy the content from a specific Iframe on my index.aspx page, then open a new window and paste it into a div.

What I got now is the code below (not my code) But I would like it less messy. Like this one:

$('#ifContent').clone().appendTo(w.document.getElementById('iFrameDiv')

Can someone help me?

var objContent = getContentWindow();

if (objContent.length > 0) {

    var w = window.open();
    var path = window.location.href.toString().toLowerCase();

    w.onerror = function () { alert('Error'); }

    w.document.open();
    w.document.writeln('<html>');
    w.document.writeln('<head><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">');
    w.document.writeln('<title>MyTitel</title>');
    w.document.writeln('<base href="' + path.replace("index.aspx", "/test") + '" />');
    w.document.writeln('<script type="text/javascript" src="javascript/jquery.min.js"></' + 'script>');
    w.document.writeln('</head>');
    w.document.writeln('<body>');


    objContent.each(function () {
        var objContainer = $('frame', this.contentDocument);

        if (objContainer.length < 1) 
        { objContainer = $(this); }

        objContainer.each(function () {
            $('body', this.contentDocument).children('form').children().each(function () {
                var strHTML = $(this).html().toString().trim().toLowerCase();
                  if ((strHTML != '') && (strHTML.indexOf('cdata') < 0) && (strHTML.indexOf('viewstate') < 0) && (strHTML.indexOf('please wait') < 0) && (strHTML.indexOf('top') < 0)) {
                    if (this.nodeName.toString().toLowerCase() != 'script') {
                        w.document.writeln('<' + this.nodeName + ' class="' + $(this).attr('class') + '" style="' + $(this).attr('style') + '">');
                        w.document.writeln($(this).html().toString().replace(new RegExp('onclick', 'g'), 'oncclick').replace(new RegExp('href', 'g'), 'hhref').replace(new RegExp('onchange', 'g'), 'oncchange'));
                        w.document.writeln('</' + this.nodeName + '>');
                    }
                }
            });
        });
    });

    w.document.writeln('</body>');
    w.document.close();
    w.focus();
    w.print();
}
else {
    top.focus();
    top.print();
};

EDIT: so I tried it like this, like suggested below by Pop Alexandru, but my "content" variable is null.

var w = window.open();
w.document.writeln('<html>');
w.document.writeln('<head><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">');
w.document.writeln('<script type="text/javascript" src="javascript/jquery.min.js"></' + 'script>');
w.document.writeln('</head>');
w.document.writeln('<body>');

    w.document.writeln('<div id="iFrameDiv">  </div>')


    var iframe = document.getElementById('ifContent');


    var iframeContent = iframe.contentWindow.document;

    //After this line my content is null
    var content = document.getElementById('iFrameDiv');


    // set contents of this div to iframe's content
    content.innerHTML = iframeContent.innerHTML;

w.document.writeln('</body>');
w.document.close();
w.focus();

Edit2:

so now its works:

w.onload = function () {
            var iframe = document.getElementById('ifContent');
            var content = w.document.getElementById('iFrameDiv');
            content.innerHTML = iframe.contentDocument.body.innerHTML;
        };

Remember ! your iframe must to be on the same domain as your page

// Get your iframe element

var iframe = document.getElementById('iframe');

// Access contents of it like this

var iframeContent = iframe.contentWindow.document;

// Get your div element

var content = document.getElementById('source');

// set contents of this div to iframe's content

content.innerHTML = iframeContent.innerHTML;

// do something when iframe is onload 

iframe.onload = function() { /* put the above code here */ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM