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,157
  • 6
  • 38
  • 61
xrcwrn
  • 5,131
  • 17
  • 63
  • 124

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
  • 56,285
  • 7
  • 72
  • 91