likes
comments
collection
share

解决谷歌浏览器使用 window.open()和document.write(),单击右键“图片另存

作者站长头像
站长
· 阅读数 287

点击图片地址,使用window.open()和document.write()打开新窗口,实现图片预览,单击右键进行保存

const imgWindow = window.open('')
imgWindow && imgWindow.document.write(`<img src='${url}' style='display: flex; margin: 0 auto'/>`)

结果发现,在部分谷歌浏览器中,新窗口打开后无法实现单击右键进行图片保存。而选“在新标签页中打开图片”却可以。为什么会这样?解决谷歌浏览器使用 window.open()和document.write(),单击右键“图片另存

在网上搜索一番,网友说是谷歌内核的原因导致,给出解决方案 xxx.document.location = "#" https://qa.1r1g.com/sf/ask/28...

const imgWindow = window.open('')
imgWindow && imgWindow.document.write(`<img src='${url}' style='display: flex; margin: 0 auto'/>`)
imgWindow.document.location = "#";

解决谷歌浏览器使用 window.open()和document.write(),单击右键“图片另存

问题解决了,但是具体是啥原因,还不清楚,而且不知道为啥要用 document.location并且设置为 #,才会起作用,希望大佬看到后解个惑