开发者

Detect if javascript dialog button clicked and redirect

开发者 https://www.devze.com 2023-02-06 15:48 出处:网络
Hey guys, I am showing a javascript dialog box to user using window.onbeforeunload to handle if user clicks back button.

Hey guys, I am showing a javascript dialog box to user using window.onbeforeunload to handle if user clicks back button. My code is working to a point, but I am struggling to redirect the user if they click 'Leave this page' (Message varies in different browsers).

// Set check var false to begin with
// This is set true on submit btns using onclick event
submitFormOkay = false;

// If we are on check-availability page
if( window.location.href.indexOf( 'che开发者_运维技巧ck-availability' ) != -1 )
{
     // If back button is clicked
     window.onbeforeunload = function()
     {
          // Only show dialog if submit btn wasn't clicked
          if (!submitFormOkay)
          {
               // Show dialog
               return "Use of the browser back button will lose form data.\nPlease    use the 'Previous' button at the bottom of the form.";

               // If 'leave' this page was clicked'
               // do 'window.location = window.location'
          }
     }
}

Thanks in advance.

David


If they click "Leave this page" then the browser will complete whatever action the user was trying to perform before the dialog popped up.

But if they do navigate Back, they will still be on your site, right? So you can just do whatever you need to do when that page is re-requested (providing it's not cached).

In other words ... detect on server-side if the user has clicked Back, and then do whatever you would have done if they had clicked Previous.

0

精彩评论

暂无评论...
验证码 换一张
取 消