28

onbeforeunloadユーザーが新しいページに移動するたびにトリガーされるはずのイベントがあります。十分に機能しますが、ユーザーが表示しているページからファイルをダウンロードするたびにChromeでもトリガーされることがわかりました。

ファイルのダウンロードによってトリガーされているため、イベントが発生しているかどうかを確認したいと思います。それを行うための最良の方法は何ですか?

編集:明確にするために、私は私が聞いているサイトを所有していませんonbeforeunload。イベントは、サードパーティのサイトにインストールされているJavascriptスニペットによってリッスンされます。

4

2 に答える 2

20

If you add download="[FILENAME]" to the a tag, it seems to prevent onbeforeunload from firing:

<a download="myfile.jpg" href="mysite.com">click me</a>

This is a much simpler solution. You can leave off the filename and just say 'download' to use the default filename. Let me point out this has the side effect of forcing redownload instead of using cache. I think this was added to chrome and ff in 2012. Not sure about safari or ie support.

于 2014-11-13T22:22:00.197 に答える