時,在Google Chrome中無法使用「保存圖像爲..」在我的應用程序中,我需要允許用戶右鍵單擊圖像以將圖像保存到磁盤。但是,我注意到,使用我的特定代碼,Google Chrome是唯一不允許用戶「將圖像另存爲...」的瀏覽器,除非用戶首先選擇Open image in new tab
,然後從那裏選擇Save image as..
。使用window.open()和document.write()
由於所有其他主流瀏覽器(包括Mobile 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>
這是一個問題,Chrome或有,我可以使用與document.write()
一起window.open()
讓Chrome瀏覽器像其他瀏覽器(即不必首先選擇Open image in new tab
另一種方式。
我必須設置'window.location。 href =「#」'那麼它爲我工作,我正在使用TypeScri PT。 – goflo