Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2011
    Thanked 0 Times in 0 Posts

    Don't allow user to exit webpage?

    I have been searching the internet for a very long time for a solution to this but to no avail. I need someone to help me out with the following.

    When a visitor tries to exit out of my webpage, a dialog box appears and asks them to stay for x amount of time. I've seen this done before, but I can't seem to cite an example here. Basically, I would like the user to stay on the webpage for at least 30 seconds. So when they go to close the window or tab, a box will appear and ask them to stay for 30 seconds. When that time is up, they can leave the page. Does anyone know how to do this? Please help me out! Thank you so much.

  2. #2
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Geraldton, Ontario
    Thanked 1 Time in 1 Post
    You can try using the onbeforeunload() function and maybe set a timer. When the timer runs in the allotted time, the function will become false. I may be working on the code for my web page.

    Anyways, I have never seen a site that actually prevents you from closing a tab or window because of a timer; it can be annoying to some people. They may all ell end up using Task Manager or restart the computer.
    Last edited by Taro; 06-01-2012 at 01:00 AM.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.

  3. #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    But you can't PREVENT them from leaving. onbeforeunload will only ask them to confirm that they want to leave, using your message. If they say they want to leave, they are gone.
    Be yourself. No one else is as qualified.

  4. #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    They can of course also simply turn off JavaScript on your page and they can then exit immediately regardless of what you try to do to prevent it.

    Of course anyone who does have trouble leaving your page will never return.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts