-2

I am trying to display a share box . my requirement is to display a bootstrap model box , if and only if user click on the close tab/browser button . if its possible how ?

HERE IS MY CODE which is not working :

var unhook=0;
 jQuery(window).bind('beforeunload', function(e) {
    if(!unhook){
    var message = "Why are you leaving?";
    $("#myModal4").modal();
    console.log(e);
    // e.returnValue = message;
    // return message;
 }
});

$(function () {
  $("a").click(function {
    var unhook=0;
  });
});

NOTE: I don't want to show simple alertbox , I have to display a modelbox where user will put the review . that's why my question is different

Kakul Sarma
  • 303
  • 1
  • 4
  • 21

2 Answers2

0

No, you can't. You can only display the canned dialog box which your browser supports.

The reason is mainly security: Imagine a malicious JavaScript which doesn't allow you to leave the page or close the browser. That's why a browser terminates all JavaScript on the page when you close the tab or the browser.

Some browsers allow you to display a message in a dialog box but not all. For details, see: Is it possible to display a custom message in the beforeunload popup?

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
-1
<button onclick="showModal()"></buton>
<div id="bootstap-modal-id">

</div>

Then

function showModal(){
  alert('something');
  $('#bootstap-modal-id').modal();
}

try this ! Happy coding :)

Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
  • i think you havnt read my question properly . its not normal button . i m asking for close tab button . – Kakul Sarma Aug 24 '16 at 10:49
  • I can see your code first, because i answered your question before your edit . in click event you can correct "function()" . – user3678907 Aug 24 '16 at 11:30