Jed*_*Jed 10 google-chrome document.write window.open save-image
在我的应用程序中,我需要允许用户右键单击图像以将图像保存到磁盘.但是,我注意到,使用我的特定代码,谷歌浏览器是唯一不允许用户"将图像另存为..."的浏览器,除非用户首先选择Open image in new tab,然后从那里选择Save image as...
由于所有其他主流浏览器(包括移动Chrome)都按预期工作,我不确定我是不是以标准/正确的方式实现我的代码,或者问题是否与Chrome有关.
例:
以下HTML是我正在做的精简版.它将允许您单击按钮以打开将包含图像的新窗口.
要测试/确认我上面描述的问题,请右键单击图像并选择Save image as..- 您应该注意到没有任何反应.但是,如果右键单击图像并选择Open image in new tab,则可以Save image as..从那里进行操作.
<html>
<head>
<title></title>
<script>
function foo() {
var tab = window.open();
tab.document.write('<p>Right-click, then click "Save image as ..."</p><img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png" />');
}
</script>
</head>
<body>
<button onclick="foo();">Open</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是Chrome存在的问题,或者是有另一种方式,我可以使用window.open()连同document.write()获得Chrome浏览器像其他浏览器(即不必首先选择Open image in new tab.
小智 12
我找到了似乎有效的解决方案.使选项卡具有位置属性.我不确定为什么需要它,但它适用于Chrome 48.
document.write('<html>
<head>
<title></title>
<script>
function foo() {
var tab = window.open();
tab.document.write('<p>Right-click, then click "Save image as ..."</p><img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png" />');
tab.document.location = "#";
}
</script>
</head>
<body>
<button onclick="foo();">Open</button>
</body>
</html>');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3815 次 |
| 最近记录: |