保存子窗口改为保存父窗口(Javascript)

kra*_*yal 1 javascript internet-explorer file save window.open

我有一些Javascript代码,可以创建一个"保存友好"版本的网页.

child = window.open("","child");
child.document.write(htmlPage);

"htmlPage"是页面的基本html,其中包含所有javascript引用,不同的标题图像引用集等.

一切都在弹出窗口中完美显示,没有运行javascript.当我单击"文件 - >另存为"时,保存的文件是父窗口及其所有javascript,并且没有子窗口的跟踪.有谁知道如何解决这个问题?我只想保存子窗口.

谢谢,-Kraryal

npd*_*oty 5

我有类似的情况(但不愿意完全放弃).我正在使用Javascript构建一个保存友好版本的网页,我希望用户下载为文本文件(在我的例子中以逗号分隔的值).我认为data:URI可以在这里提供帮助.

//construct the csvOutput in Javascript first
var popup = window.open("data:application/octet-stream," + encodeURIComponent(csvOutput), "child");
//no need to document.write() anything in the child window
Run Code Online (Sandbox Code Playgroud)

在Firefox中,这甚至不会弹出窗口,只是询问用户是否要保存文件,并将其保存为.part文件.不完全理想,但至少它保存文件而不会弹出不必要的窗口.

或者,我们可以使用text/plain MIME类型:

//construct the csvOutput in Javascript first
var popup = window.open("data:text/plain;charset=utf-8," + encodeURIComponent(csvOutput), "child");
Run Code Online (Sandbox Code Playgroud)

在Firefox中,这会弹出一个新窗口,但它默认保存为ASCII文本,没有任何父窗口或任何换行.这可能是我将要使用的.

看起来这在IE中不起作用.IE 8是唯一支持data:URI的版本,它对可以使用的位置有一系列限制.对于IE,您可以查看execCommand.

感谢这个tek-tip线程关于数据URI方案维基百科文章.