保存子窗口会保存父窗口(Javascript)

发布于 2024-07-23 04:48:25 字数 368 浏览 2 评论 0 原文

我有一些 Javascript 代码,可以创建网页的“保存友好”版本。

<代码> child = window.open("","child");
child.document.write(htmlPage);

“htmlPage”是页面的基本 html,其中删除了所有 javascript 引用、一组不同的标题图像引用等。

所有内容都完美地显示在弹出窗口中,无需运行 javascript。 当我单击“文件->另存为”时,保存的文件是父窗口及其所有 JavaScript,并且没有子窗口的痕迹。 有谁知道如何解决这个问题? 我只想保存子窗口。

谢谢, -克拉亚尔

I have a bit of Javascript code that creates a "save friendly" version of a webpage.


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

"htmlPage" is the basic html of the page with all the javascript references taken out, a different set of header images references, etc.

Everything displays perfectly in the popup window, with no javascript running.
When I click on "File->Save As", the saved file is the parent window, along with all of its javascript, and with no trace of the child window. Does anyone know how to solve this problem? I want to save only the child window.

Thanks,
-Kraryal

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单身狗的梦 2024-07-30 04:48:25

我也有类似的情况(但不愿意完全放弃)。 我正在使用 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

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

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

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

在 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.

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

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:

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

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.

没企图 2024-07-30 04:48:25

当您保存页面时,它将保存原始 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.

巡山小妖精 2024-07-30 04:48:25

对于仅 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....

请恋爱 2024-07-30 04:48:25

唉,看来我没有足够的声誉来为我的问题添加评论,所以我将在这里添加它。

此 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文