-1

I have a scenario where I need to capture the response received from a dialog box INVOKED by IE from an onbeforeunload event handler. The dialog box is a familiar one,

"Are you sure you want to navigate away from this page? You will lose any unsaved modifications to this document. Press OK to continue or Cancel to stay on the current page."

I will emphasize for the sake of anyone misinterpreting this question, that the dialog box I am interested in capturing is THE BROWSER's dialog window, NOT a dialog window that one might create using a third party library such as JavaScript, Telerik or etc. as the source.

I am really not that concerned with capturing a click of the OK button (although it would be a plus) but more concerned with the clicking of the Cancel button because it requires more than simply retaining the user on the current page and our company would like to implement some additional logic on click of the Cancel button.

I believe I have diligently searched all resources available inclusive of Stack Overflow but I cannot find any specific answer or information regarding how to acquire the handle on the dialog and determine which button the user clicked.

Since our companies primary browser for this application is IE, the question is only with respect to IE. Can this be done and if so, where might I find the appropriate documentation or acquire a lead? Seeing as I haven't found any definitive answer to date, I am presuming it is not possible but would like others responses to this.

EDITED: This question is specific to acquiring the users input (Using IE's Native Dialog or the Browser's native prompt). E.g. Again, Not Dialog boxes or Prompts developed by (You) as the Developer.

NOTE: --> There is a very significant difference in that developing with a Third Party library you as the Developer already have a reference to the object because YOU create it. Whereas you don't have an obvious handle to a Dialog Box created by the Browser.

Dave
  • 10,748
  • 3
  • 43
  • 54
Mark
  • 1,667
  • 2
  • 24
  • 51
  • Of course it can be done, what kind of dialog are you using? This can be done with simple window.confirm if statement. What have you tried? – Esko Sep 09 '14 at 11:35
  • 2
    You can easily do this .. var res = confirm("Do you really want to do this?? "); if (res === true) {// user clicks Ok } else{// user clicks Cancel} – xxbinxx Sep 09 '14 at 11:36
  • 2
    Ok, let me re-emphasize the issue because someone apparently determined that this question showed no relevant research before posting but whomever made that determination and was fast handed to give a -1 on the question; they should probably "Read" the question thoroughly as well before being trigger happy. Yes, I am aware of both of the techniques for acquiring the users input (Using your own created dialogs) The question was specific to a dialog that is generated by the Browser, not the developer. – Mark Sep 09 '14 at 11:41
  • @Mark You should now write this thing in your question. Please edit it and I don't think you can get value from the browser's native prompt – xxbinxx Sep 09 '14 at 11:47
  • I don't understand why this person @FedericHamidi marked this as duplicate ... just look at the comments. He is asking for something else.. – xxbinxx Sep 09 '14 at 11:49
  • Thank you xK0nB1n, from what little information I was able to acquire it didn't appear to be possible but I wanted to run this question past others to be certain. I thought I was fairly clear and definitive in the question but I can certainly edit it again. – Mark Sep 09 '14 at 11:51
  • @xK0nB1n, from the comment exchange, don't you think *you* have a problem understanding what the questioner wants, not me? ;) – Frédéric Hamidi Sep 09 '14 at 11:55
  • @Mark, it *is* possible (sort of). See the comment under the accepted answer in the duplicate I linked to. – Frédéric Hamidi Sep 09 '14 at 11:55
  • @FrédéricHamidi sir he mentioned that he wants the response from the dialog that is not generated by user/developer and the question you referred does not provide answer to this question. You please have a conversation with Mark for clarification 'coz I may be wrong. – xxbinxx Sep 09 '14 at 12:00
  • @FrédéricHamidi, I checked the question you referenced as a duplicate but it doesn't appear to address the specifics of what I asked. It addresses the question of capturing user input from a Dialog that the person posting the question had created. My question is specific to browsers native dialog prompts, which are not the same as those that a Developer would create. So if xK0nB1n proposes his response as an answer and no one else can provide an answer, I'll accept that. But I don't believe this question is a duplicate. Maybe somewhat similar in context but 2 entirely different questions. – Mark Sep 11 '14 at 15:44
  • @Mark, okay, I thought your question was about the `beforeunload` browser-specific dialog since you quoted one verbatim, but it looks like either I'm missing something or both you and xK0nB1n are wrong. As I'm outnumbered, I will use Mjölnir again to reopen your question, but I hope I won't have to regret it. In passing, I would advise you elaborate on why your question is definitely not a duplicate, in your post itself. It will prevent others from making the same mistake as I, if any. – Frédéric Hamidi Sep 11 '14 at 15:52
  • The dialog box you describe is one that seems to be created by the page/script being viewed, can you clarify or perhaps give an example of where such a dialog exists that is NOT created by the developer? – John U Sep 11 '14 at 15:58
  • For example, say you were to encapsulate another webform within an IFrame and navigate within the embedded form. And upon navigation to another area within your site that is encapsulating the Webform you may potentially receive the message similar to what I've described. I guess that would be the best example I can think of off hand. Does that make it any clearer? – Mark Sep 11 '14 at 16:54

1 Answers1

3

You can't actually get the result of what was clicked, but you can use setTimeout in your onbeforeunload event to run some code if the user chooses to stay on the page.

For example:

var afterStayFunction = function() {
    $("body").append("Thanks for staying.");
};

$(window).on("beforeunload", function(e) {
    var prompt = "Are you sure you want to leave?";
    e.returnValue = prompt;
    setTimeout(function() { afterStayFunction(); });
    return e.returnValue;
});

Live example here

Dave
  • 10,748
  • 3
  • 43
  • 54
  • Thank you! The definitive answer of No you can't get the result of what was clicked along with an alternative workaround is a workable solution for me. I can finagle with the setTimeout recommendation and the required logic to produce some type of solution that should be acceptable for the Company. I appreciate the feedback. – Mark Sep 11 '14 at 17:08