Solve Google Chrome's use of window.open() and document.write(), right-click "Save Image As…" doesn't work

Time:2022-9-22

Click the image address, use window.open() and document.write() to open a new window, realize the image preview, right-click to save

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

It was found that, in some Google Chrome browsers, it is not possible to right-click to save a picture after a new window is opened. Instead, select &quot;Open image in new tab&quot;. Why is this happening?

After a search on the Internet, netizens said that it was caused by the Google kernel, and gave a solutionxxx.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 = "#";

The problem is solved, but the specific reason is not clear, and I don't know why to use it document.locationand set to#, it will work, I hope the boss will clear up the confusion after seeing it

Recommended Today

Laohuangli fortune-telling interface implementation and code example

The old almanac fortune-telling interface provides the old almanac query, and the almanac daily fortune-telling and taboo query. Interface name: Laohuangli fortune-telling interface Interface Platform: Aggregate Data (http://www.juhe.cn) interface address:http://v.juhe.cn/laohuangli/d Supported formats: JSON/XML Request method: HTTP GET/POST Request example:http://v.juhe.cn/laohuangli/d… Example of JSON return of the old yellow calendar fortune-telling interface: { “reason”: “successed”, “result”: { […]