Chrome:JavaScript window.open可以保存 [英] Chrome: JavaScript window.open to be Save-able

查看:157
本文介绍了Chrome:JavaScript window.open可以保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个用HTML和JavaScript编写的FTP客户端。这部分工作。但是,如果用户可以将清单复制到剪贴板中,那将会很好。原来,剪贴板的东西在JS中并不那么容易(除此之外,列表可能很大)。所以更好的办法是弹出一个带有生成列表的窗口,然后用户可以选择复制粘贴,或将页面保存到磁盘。



目前我确实: p>

 
my_window = window.open(,Copy List);
my_window.document.write('< pre> \\ n'+ string +'< pre>');
my_window.document.close();

其中有效。我收到一个新选项卡,并且我在字符串中生成的列表显示效果很好。



但Chrome禁用/隐藏保存页面选项。如果用户可以保存页面(html或txt),那将会很好。打开一个窗口/选项卡并让它们保存内容需要什么魔力?



由于我们使用WebSockets(key1 / key2),所以只能在Chrome中使用,没有其他浏览器需要。

解决方案



window.open(data:text / plain; base64,+ btoa(theCode))


Imagine a FTP client written in HTML and JavaScript. This part works. But it would be nice if user can "copy the listing" into clipboard. Turns out that clipboard stuff is not so easy in JS (besides, listings can be huge). So better is to pop up a window with the generated listing, then user can chose to Copy'Paste, or Save the page to disk.

Currently I do:

    my_window = window.open("", "Copy List");
    my_window.document.write('<pre>\n'+string+'</pre>');
    my_window.document.close();

Which works. I get a new tab, and the listing I have generated in "string" displays nicely.

But Chrome disables/greyes-out the "Save Page" option. It would be nice if user can save the page (html or txt). What magic is required to open a window/tab and let them save the content?

Since we use WebSockets (key1/key2) this only works in Chrome, no other browsers needed.

解决方案

Way after the fact but you can use a data URI for this:

window.open("data:text/plain;base64,"+btoa(theCode))

这篇关于Chrome:JavaScript window.open可以保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆