简体   繁体   English

将当前 iframe 内容导出为 HTML 文件

[英]Export current iframe content as HTML file

Here is the thing - I'm trying to create kind of email signature generator.事情是这样的——我正在尝试创建一种 email 签名生成器。 Most of the index file, is a pure HTML code + tiny javascript.大多数索引文件,是纯 HTML 代码 + 微小的 javascript。 Inside index file I'm embeding email signature template as an iframe/email_signature.html file.在索引文件中,我将 email 签名模板作为 iframe/email_signature.html 文件嵌入。 So it looks as follows:所以它看起来如下:

<iframe src="email_signature.html" width="100%" height="350" frameborder="0" id="testing" name="emailSignature"></iframe>

Then I'm trying to download current content of this iframe by executing this code:然后我尝试通过执行以下代码下载此 iframe 的当前内容:

$(document).ready(function() {
        const innerFrame = document.getElementById('testing').contentWindow.document.body.innerHTML;
    $("#cmd").click(function(){
        exportFile('new-file.html', $innerFrame.innerHTML);
    }); 
});

but it doesn't work.但它不起作用。 Here is the example fiddle:这是示例小提琴:

https://jsfiddle.net/py1tojmc/ https://jsfiddle.net/py1tojmc/

What I'm doing wrong here?我在这里做错了什么?

From what I can see in the dev console opening your jsfiddle, i don't see any "event" attached to your download button.从我在打开您的 jsfiddle 的开发控制台中可以看到,我没有看到任何“事件”附加到您的下载按钮。 在此处输入图像描述

I can't tell you why this is happening (I'm not that expert), but maybe the call to .contentWindow.document.body.innerHTML;我不能告诉你为什么会发生这种情况(我不是那个专家),但也许是对.contentWindow.document.body.innerHTML; is somehow changing the DOM context to the iframe (just a guess), and it seems you cannot access the button id #cmd anymore.以某种方式将 DOM 上下文更改为 iframe(只是猜测),并且您似乎无法再访问按钮 ID #cmd

Try changing your code like this ans see if it works:尝试像这样更改您的代码,看看它是否有效:

$(document).ready(function() {
    $("#cmd").click(function(){
        const innerFrame = document.getElementById('testing').contentWindow.document.body.innerHTML;
        exportFile('new-file.html', $innerFrame.innerHTML);
    }); 
});

Yeah looks like the button event won't fire.是的,看起来按钮事件不会触发。 But could be cross origin security:但可能是跨源安全性:

SecurityError: Permission denied to access property "document" on cross-origin object

But that could just be the jsfiddle, can you put an alert('cmd button works');但这可能只是 jsfiddle,你能放一个alert('cmd button works'); just to make sure your button fires?只是为了确保您的按钮触发?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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