0

I want to show an alert box on the beforeunload event. I am using the following code for that. Problem with this, is that it does not show an alert box on this event.

$(window).bind('beforeunload', function() {
                alert($(location).attr('hostname'));
            });

How can this problem be resolved?

Joe DF
  • 5,438
  • 6
  • 41
  • 63
xrcwrn
  • 5,339
  • 17
  • 68
  • 129

1 Answers1

1
$(window).on('beforeunload', function(){
  return location.host;
});

This is what you have to do.

Basically you can also use without jquery

window.onbeforeunload=function(){ return location.hostname; };
Amit Joki
  • 58,320
  • 7
  • 77
  • 95