Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    12
    Thanks
    2
    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
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    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
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,184
    Thanks
    80
    Thanked 4,451 Times in 4,416 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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 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.
    Stephen
    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
    •