-1

Possible Duplicate:
How do you prevent javascript page from navigating away?
How do I stop a page from unloading (navigating away) in JS?

I have been on several websites, sorry I cant provide the url, that do not let you leave. No matter what you do, besides force quitting (closing the page, navigating to a new url, regular quitting, you cant leave). These websites always have an alert box that asks you a question like: "do you want to leave?" but both answers (yes and cancel) leave you back on their page. I would just like to know how they do this. will not use this in my website, I am not a spammer just wondering

Community
  • 1
  • 1
user1354917
  • 105
  • 14

1 Answers1

4

With this code:

window.onbeforeunload = function(){
    return 'Are you sure you wanna leave my website?';//Displays an confirm box and, if you click cancel, you will stay on the page. Otherwise, you'll leave the page
}

However, putting this code on your website will make your viewers very angry. Therefore I warn you against it

JakeParis
  • 11,056
  • 3
  • 42
  • 65
Danilo Valente
  • 11,270
  • 8
  • 53
  • 67