<a href="https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h" id="leave">click here to leave the page</a>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Changes made may not be saved.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
$("#leave").click(function() {
$(window).bind('beforeunload', function() {
return 'Changes you made may not be Saved';
});
});
Asked
Active
Viewed 2,886 times
7

Sang Suantak
- 5,213
- 1
- 27
- 46

vishag v.j
- 101
- 1
- 6
-
1You can't do that. – Teemu Aug 29 '18 at 04:14
-
Closest you will get methinks is https://stackoverflow.com/questions/20253246/adding-click-event-for-a-button-created-dynamically-using-jquery. OR Just make you check if a 'saved' variable is set. – Gavin Simpson Aug 29 '18 at 04:22
-
could you please confirm if you need that "beforeunload" event or it is just for example? if you don't need it then it's possible to show model otherwise not. – Ram Singh Aug 29 '18 at 04:36
2 Answers
1
You could prevent the leave
link's default behavior, display the dialog and use timeout to redirect the page in a few seconds.
Example:
$("#leave").click(function(e) {
e.preventDefault();
var link = this.href;
//code to show dialog
window.setTimeout(function() {
window.location.href = link;
}, 2000);
});
Also, you need to wrap the script codes in a script
html tag.

Sang Suantak
- 5,213
- 1
- 27
- 46
0
Why cant you set up redirecting logic inside the modal button and let the a
element display the modal.It seems like a simpler solution.
$("#leave").click(e => {
e.preventDefault()
$("#myModal").modal({
show: true
})
})
$("#proceedLeaveBtn").click(() => {
window.location.href = "https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_modal&stacked=h"
})
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<a href="#" id="leave">click here to leave the page</a>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Changes made may not be saved.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button id="proceedLeaveBtn" type="button" class="btn btn-default" data-dismiss="modal">Proceed</button>
</div>
</div>
</div>
</div>
data-href
attribute inside the button element and retrive it inside the click handler.Its up to you.
marc_s
- 732,580
- 175
- 1,330
- 1,459

Jan Ciołek
- 1,796
- 3
- 17
- 32