1

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> 
1
  • Please note that almost all html tags will not shown untill you put it in a code block. So, people will not be able to read the code. Try using the buttons in the question form wisely. Commented Mar 16, 2017 at 13:50

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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> 

3 Comments

my include of jquery source code is not showing up on code view in html browser. Should that have 'https://' added to the src statement?
If it is from same origin(your own server, no need). Otherwise add the full url
<!DOCTYPE html> <html> <script src="code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <body> <a href="science.gov" id="navigate">science</a> </body> <script> $(window).on('beforeunload', function(){ return prompt('Are you sure you want to leave?')}); </script> </html> This is not producing a prompt.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.