jqueryonbeforeunloadwindow.onunload

Jquery Onbeforeonload


This simple code from jquery is not picking up the unload event. The console says caching is being disabled.

I want users prompted on clicking on each link on my page that leave page, but does not prompt when they close page. Any suggestions.

<html>

<script src="jquery-3.1.1.min.js"></script>

<body>

<a href="https://www.science.gov" id="navigate">science</a>

</body>

<script>

    $(window).on('beforeunload', function(){
    return('Are you sure you want to leave?')};

</script>

</html>

Solution

  • there is an error in your code, you mis a ) at the end

    Uncaught SyntaxError: missing ) after argument list

    change your code to: $(window).on('beforeunload', function(){ return('Are you sure you want to leave?')});