将内容从Iframe复制到div

时间:2018-05-03 11:50:49

标签: javascript jquery asp.net iframe webforms

我想在index.aspx页面上复制特定Iframe中的内容,然后打开一个新窗口并将其粘贴到div中。

我现在得到的是下面的代码(不是我的代码)但是我希望它不那么混乱。 像这样:

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

有人可以帮助我吗?

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();
};
编辑:所以我尝试了这样,就像Pop Alexandru下面提到的那样,但是我的“内容”变量是空的。

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:

现在它的作品:

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

1 个答案:

答案 0 :(得分:1)

  

记住!您的iframe必须与您的网页位于同一个域

// 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 */ }