BumbleBee
BumbleBee

Reputation: 10779

prevent Page refresh after pop-up

How can i prevent page refresh when the user clicks on the OK button of the javascript alert window.

 <asp:Button ID="btnComplete" runat="server" Text="Complete" OnClientClick ="verify_complete()" />


 function verify_complete() {
        if (!chkCmnts()) {
            alert("Categories/comments are required. ");
            return false;
                         }
                             }

Thanks

BB

Upvotes: 0

Views: 3583

Answers (2)

Icarus
Icarus

Reputation: 63956

You are already returning false from the JS so you need to change your markup to:

<asp:Button ID="btnComplete" runat="server" Text="Complete" OnClientClick ="return verify_complete()" />

If the verify_complete() function returns true it will post back, if it doesn't, it won't.

Upvotes: 1

FloydThreepwood
FloydThreepwood

Reputation: 1585

You can not prevent this, but you can notify the user via a popup that he should not reload. This is done with the onbeforeunload window event, best documented at MDN.

add this to your popup window in a Script-Tag

var controllVar = 1;

add this to your parent window. (popup is the return of window.open(...)). Here is actually some intensive error testing, because IE in it's various flavours shows more than one Bug.

window.onbeforeunload = function (e) {
  //here we try accessing the popup
  try {
      if(!popup && popup.controllVar != 1) {
          return;
      }
  } catch(e) {
     return;
  }

  e = e || window.event;

  // For IE and Firefox prior to version 4
  if (e) {
    e.returnValue = 'YOUR ERROR MESSAGE';
  }

  // For Safari
  return 'YOUR ERROR MESSAGE';
};

This will behave exactly like here on Stackoverflow, if you add some text to the answer textarea and than try to leave the page.

Upvotes: 0

Related Questions