Skip to content

Commit a26455b

Browse files
committed
Add button to clear the beforeunload listener
1 parent 3e34432 commit a26455b

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

testcases/cancel-beforeunload.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
<head>
77
<meta charset="UTF-8">
88
<script type="application/javascript"><!--
9-
window.addEventListener('beforeunload', function(event) {
9+
function onBeforeUnload(event) {
1010
console.log('beforeunload');
1111
event.preventDefault();
1212
event.returnValue = '';
13-
});
13+
}
14+
window.addEventListener('beforeunload', onBeforeUnload);
1415
console.log('beforeunload listener is registered.');
1516

1617
function openTabs(url, count) {
@@ -23,6 +24,10 @@
2324
</head>
2425
<body>
2526
<p>This page should show a confirmation before closed or reloaded.</p>
27+
<p><button onclick="window.removeEventListener('beforeunload', onBeforeUnload)"
28+
onkeypress="if (event.key == 'Enter') window.removeEventListener('beforeunload', onBeforeUnload);"
29+
>Clear unload listener</button></p>
30+
<hr>
2631
<p><button data-count="5"
2732
data-url="about:blank"
2833
onclick="openTabs(this.dataset.url, this.dataset.count)"

0 commit comments

Comments
 (0)