MEV
MEV

Reputation: 11

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>

Upvotes: 1

Views: 598

Answers (2)

Sagar V
Sagar V

Reputation: 12478

return is not a function and it will not prompt. Try return prompt();

<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 prompt('Are you sure you want to leave?');});
 </script> </html>

Upvotes: 0

Ricardo van Laarhoven
Ricardo van Laarhoven

Reputation: 788

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?')});

Upvotes: 1

Related Questions