保存子窗口会保存父窗口(Javascript)
我有一些 Javascript 代码,可以创建网页的“保存友好”版本。
<代码>
child = window.open("","child");
child.document.write(htmlPage);
“htmlPage”是页面的基本 html,其中删除了所有 javascript 引用、一组不同的标题图像引用等。
所有内容都完美地显示在弹出窗口中,无需运行 javascript。 当我单击“文件->另存为”时,保存的文件是父窗口及其所有 JavaScript,并且没有子窗口的痕迹。 有谁知道如何解决这个问题? 我只想保存子窗口。
谢谢, -克拉亚尔
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也有类似的情况(但不愿意完全放弃)。 我正在使用 Javascript 构建一个易于保存的网页版本,我希望用户将其下载为文本文件(在我的例子中为逗号分隔值)。 我认为
data:
URI 可以在这里提供帮助。在 Firefox 中,这甚至不会弹出窗口,只是询问用户是否要保存文件,并将其保存为 .part 文件。 不太理想,但至少它保存了文件而不会弹出不必要的窗口。
或者,我们可以使用 text/plain MIME 类型:
在 Firefox 中,这会弹出一个新窗口,但默认情况下它会保存为 ASCII 文本,没有任何父窗口的缺陷或任何换行。 这可能就是我会用的。
但看起来这在 IE 中不起作用。 IE 8 是唯一支持
data:
URI 的版本,并且它具有 对其使用地点的一系列限制。 对于 IE,您可以查看 execCommand。感谢此 tek-tip 帖子 和 有关数据 URI 方案的维基百科文章。
I had a similar situation (but wasn't willing to give up altogether). I'm constructing a save-friendly version of a webpage using Javascript that I want the user to download as a text file (comma-separated values, in my case). I think
data:
URIs can help here.In Firefox, this doesn't pop up a window even, just asks the user if they want to save the file, and saves it as a .part file. Not exactly ideal, but at least it saves the file without popping up an unnecessary window.
Alternatively, we can use the text/plain MIME type:
In Firefox, this does pop open a new window, but then it's saved by default as ASCII text, without any of the cruft of the parent window or any line-wrapping. This is probably what I will use.
It looks like this won't work in IE though. IE 8 is the only version that supports
data:
URIs, and it has a series of restrictions on where it can be used. For IE, you might look at execCommand.Thanks to this tek-tip thread and the Wikipedia article on the data URI scheme.
当您保存页面时,它将保存原始 URL 内容(例如,就像您下载了新副本一样)
如果您想要“清理”版本,则需要在服务器上生成该版本,并使用该版本打开弹出窗口URL 作为第一个参数。
When you save the page it will save the original URL content (e.g. just as if you downloaded a fresh copy)
If you want a "cleansed" version, you'll need to generate that version on the server, and open the popup with that URL as the first param.
对于仅 Windows/IE 版本,请参阅此处:http://p2p.wrox.com/javascript-how/3193-how-do-you-save-html-page-your-local-hd.html
我知道,太糟糕了,但以防万一它像一个仅限 IE 的内部网......
for a windows/IE only version see here: http://p2p.wrox.com/javascript-how/3193-how-do-you-save-html-page-your-local-hd.html
I know, terrible, but just in case it's for like an IE-only intranet....
唉,看来我没有足够的声誉来为我的问题添加评论,所以我将在这里添加它。
此 JavaScript 已针对 Internet Explorer 和 Safari 部署。 托管应用程序并不总是能够访问本地文件系统。
用户可以标记他们正在使用的页面,然后保存以供以后打印或通过电子邮件发送。 我们告诉他们将其保存为单个文件存档,这样它实际上就可以是邮件。 保存对话框可以做到这一点,并且用户可以将文件放在他们有权访问的位置,这就是我们使用它的原因。
看起来简单的方法是将新的 html 从 javascript 写入本地目录,但我们不能总是这样做。 还有其他想法吗? 感谢你目前的帮助。
Alas, it seems I haven't enough reputation to add comments to my question, so I'll add this here.
This javascript is deployed for both Internet Explorer and Safari. The hosting application doesn't always have access to the local file system.
The users can mark up the page they are working with, then save it for later printing or emailing. We tell them to save it as a single-file archive so it can actually be mail. The save dialog can do that, and the user can put the file somewhere they have access t, so that's why we used it.
It seems the simple way would be to write the new html from the javascript to the local directory, but we can't always do that. Any other ideas? Thanks for the help so far.