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 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    disable all refresh events using javascript

    I need disable all refresh events

    Example.
    1. F5,
    2. Mouse right click option (refresh/reload),
    3. CTRL+R,
    4. Menu bar -> view -> refresh and
    5. URL refresh/reload button

    How to disable these all events by using javascript...

    Advance thanks....

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by ashok.5656 View Post
    I need disable all refresh events

    Example.
    1. F5,
    2. Mouse right click option (refresh/reload),
    3. CTRL+R,
    4. Menu bar -> view -> refresh and
    5. URL refresh/reload button

    How to disable these all events by using javascript...

    Advance thanks....
    Not possible. Why do you want to do that? You may not interfere with the normal operation of the user's operating system, nor the default behaviour of the browser.
    It is possible to block the F5 key but you simply cannot control the refresh button specific to your browsers' toolbar, nor the mouse right-click option.

    Code:
    <script type = "text/javascript">
    
    document.onkeydown = function(ev) {	
    var key;
    ev = ev || event;
    key = ev.keyCode;
    if (key == 116) {
    return false;  // disable F5 key
    }
    
    </script>

    You might do something with cookies or local storage, say to preserve the status of a button (now disabled) upon refresh, but the user can clear his cookies and/or simply re-start the page over.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 04-19-2013 at 09:30 AM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you tell us what the problem is that is being caused by the refresh then we might be able to tell you how to fix it so that refreshing doesn't cause the problem.
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    And even if you could prevent all those, the user could simply close the browser and open it again to the same page.

    What a pointless exercise.
    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.

  • #5
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    need help disable all refresh events using javascript

    Quote Originally Posted by felgall View Post
    If you tell us what the problem is that is being caused by the refresh then we might be able to tell you how to fix it so that refreshing doesn't cause the problem.
    i am trying to create online examination page with random questions and timer in reverse.if i refresh the page means the time will start from initial stage and the question will change.so i need some suggestions or guidance to avoide these problems....

    thanks

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by ashok.5656 View Post
    i am trying to create online examination page with random questions and timer in reverse.if i refresh the page means the time will start from initial stage and the question will change.so i need some suggestions or guidance to avoide these problems....

    thanks
    You must use server-side code for this. As Old Pedant says, the user could simply close the browser and open it again to the same page.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Welllllll....maybe not.

    It wouldn't stop a sophisticated user from faking you out, but...

    What you could do is store the start time and the order of the questions in a cookie.

    And then, if the page is refreshed, you check to see if the cookie is already there and, if so, you rebuild the page based on the info in the cookie.

    But it's a pretty poor solution. Using server-side code (PHP/ASP/JSP) would be far far better.
    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.

  • Users who have thanked Old Pedant for this post:

    ashok.5656 (04-22-2013)

  • #8
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Welllllll....maybe not.

    It wouldn't stop a sophisticated user from faking you out, but...

    What you could do is store the start time and the order of the questions in a cookie.

    And then, if the page is refreshed, you check to see if the cookie is already there and, if so, you rebuild the page based on the info in the cookie.

    But it's a pretty poor solution. Using server-side code (PHP/ASP/JSP) would be far far better.

    Thanks for Your post.I will try the cookies...

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Quote Originally Posted by ashok.5656 View Post
    Thanks for Your post.I will try the cookies...
    A poor solution, and a waste of time IMO. Any user with the slightest knowledege will simply delete the cookie.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    A poor solution, and a waste of time IMO. Any user with the slightest knowledege will simply delete the cookie.
    and anyone with more than slight knowledge will edit the cookie just before they finish or just before the time limit so as to reset the start time.
    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.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why not just warn the user that the examination will restart using window.onbeforeunload event?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Quote Originally Posted by glenngv View Post
    Why not just warn the user that the examination will restart using window.onbeforeunload event?
    Because the user will have seen all the questions, looked up the answers, and then restarted the examination to achieve a perfect score.

    In other words, the cheater *wants* to restart the examination.

    And all of this is why a client-side-only solution isn't really possible.
    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.

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by glenngv View Post
    Why not just warn the user that the examination will restart using window.onbeforeunload event?
    A lot of people have that event disabled in their browser because it is most often used in annoying ways rather than for its intended purpose.
    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.


  •  

    Posting Permissions

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