+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / intercepting page leave (refresh, back, close, etc)

intercepting page leave (refresh, back, close, etc)

3 posts, 0 answered
  1. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    12 Nov 2008
    Link to this post
    this isn't exactly a sitefinity question, but I noticed that when editing pages, if I attempt to go back, refresh, or close the browser, I get a javascript prompt asking if I want to confirm the action. How exactly is this done?

    Clearly it has to be some kind of javascript, but is there some example code on how this is done? I wish to acheive the same functionality for a specific page on my public website to prevent users from refreshing the page...

    many thanks!
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    12 Nov 2008
    Link to this post
    well as usual it was easier than I thought :)

    here's a great resource that worked perfectly:

    http://www.openjs.com/scripts/events/exit_confirmation.php
  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    12 Nov 2008
    Link to this post
    Hello SelArom,

    Thanks for sharing the solution :)
    Here is something else that might be interesting - intercepting keyboard events with Javascript (for example the F5 key):
    function CheckKeys() 
     
     var vbKeyBack = 8; 
     var vbF5 = 116; 
     var vbKeyRButton = 2; 
     var osrcelement = event.srcElement; 
     var strSrcElement = osrcelement.name; 
     var intSrcElementLen = 0; 
     var blnSrcElementisDefined = true
     var blnBKSpaceOK = false
     
     // Check to see if the object is valid 
     if (strSrcElement != null
      { 
       intSrcElementLen = strSrcElement.length; 
      } 
     
     // Evaluate source element name length 
     if (intSrcElementLen == 0) 
      { 
       blnSrcElementisDefined = false 
      } 
     // If we have a valid source element and it has a name 
     // determine if it is a text object so we can allow the backspace 
     if (blnSrcElementisDefined == true
      { 
       strSrcElement = strSrcElement.substr(0,3) 
       if (strSrcElement.toLowerCase() == 'txt' && strSrcElement.toLowerCase() 
    == 'mem' &&  event.keyCode == vbKeyBack) 
        { 
         blnBKSpaceOK = true
        } 
      } 
     //  if the F5 key was pressed then bypass it not allowing the user 
     // to refresh the page 
     if ((blnCancel == true) && (event.keyCode == vbF5 )) 
      { 
       event.keyCode=0; 
       event.returnValue=false
      } 
     else 
      // Well it was not the F5 key so if it was the Backspace key 
      // pressed and we are not on a text field disable it 
      if ( event.keyCode == vbKeyBack && blnBKSpaceOK != true
       { 
        event.keyCode=0; 
        event.returnValue=false
       } 
     

    Regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
3 posts, 0 answered