javascript - Close pop up on back button -
i want close pop on click of button mobile. implemented using onhashchange:
window.onhashchange = function (event) { }; in case, if pop opened multiple times on click of button, opens , closes modal pop up. but, want modal pop close on first , navigate prev page on next back.
i tried using onbeforeunload, show alert leave or stay on page.
$(window).bind('beforeunload', function(e) { return false; }); what best way close pop on button , redirect prev page on next back?
bootply.com down when testing answer. see inline script , comments @ bottom of code below. rest twitter bootstrap boilerplate test locally.
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>modal.html</title> <!-- bootstrap --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"> <!-- html5 shim , respond.js ie8 support of html5 elements , media queries --> <!-- warning: respond.js doesn't work if view page via file:// --> <!--[if lt ie 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <p>if press button now, should return whatever page on before page.</p> <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#mymodal">show me modal!</button> <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">close</span></button> <h4 class="modal-title" id="mymodallabel">modal title</h4> </div> <div class="modal-body"> <p>if press web browser's button or modal's close buttons, modal close , hash return "modal.html#modalclosed".</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script type="text/javascript"> // immutable hash state identifiers. var closedmodalhashstateid = "#modalclosed"; var openmodalhashstateid = "#modalopen"; /* updating hash state creates new entry * in web browser's history. latest entry in web browser's * history "modal.html#modalclosed". */ window.location.hash = closedmodalhashstateid; /* latest entry in web browser's history "modal.html#modalopen". * entry before "modal.html#modalclosed". */ $('#mymodal').on('show.bs.modal', function(e) { window.location.hash = openmodalhashstateid; }); /* when user closes modal using twitter bootstrap ui, * return previous entry in web * browser's history, "modal.html#modalclosed". same thing * happens when user clicks web browser's button. */ $('#mymodal').on('hide.bs.modal', function(e) { window.history.back(); }); </script> </body> </html>
Comments
Post a Comment