如圖,評論還未提交,關閉瀏覽器時會提示。 這是怎么實現的?
慕神8447489
2023-04-09 18:14:02
TA貢獻1788條經驗 獲得超4個贊
主要是監聽了window
的onbeforeunload
事件,對一般的瀏覽器來說如果在這個事件監聽時返回一個字符串,它就會彈出一個對話框,但有的瀏覽器是使用event.returnValue
,你可以使用以下的兼容性代碼
window.onbeforeunload = function (e) { var message = 'some word'; e = e || window.event; if (e) { e.returnValue = message; } return message; };
舉報