Prevent JS-Window from closing without showing a prompt - javascript

In my MS CRM 4.0-application, there are several things to do before the window is closed.
So I have to stop the window from closing in the onbeforeunload-function and do my stuff in different locations.
This works already, but only if I show the dialog by returning some message in my onbeforeunload-function and the user chooses "stay on page" (or something like this)
Is it possible to skip this dialog and stop the window from closing?
I know there are several reasons, why this shouldn't work, but, e.g. in my CRM-application it is neccessary.

Related

How to confirm closing browser window?

The question is
How to show a confirmation box on closing a browser window ?
This question has been asked many times before, and previous answers, such as https://stackoverflow.com/a/333673/1442181 suggest to use the onbeforeunload event.
But, according to http://developer.mozilla.org/en-US/docs/Web/Events/beforeunload, "browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with, or may even not display them at all."
I have a preview page after the user has filled some form, and I would like to show a confirmation prompt when the user leaves the page (except by clicking "back to edit" or "submit"). The preview page cannot be interacted with, so previous solutions seem to not work e.g. in the current firefox version.
(How to avoid the prompt on these two links is not part of the question and answerred in detail elsewhere.)

How to select “Cancel” by default in confirm box when closing a browser tab

I know there is a very similiar question (JavaScript: How to select "Cancel" by default in confirm box?) on this site, but I felt like I did not get a good answer out of that one as my premises are not the same.
Problem description: I am writing a web browser Javascript popup window that is supposed to take some information as input and the user can choose between "OK" to process the information and continue with whatever, or "Cancel" to close the window and go back to whatever. So far so good. The problem is that some users press "X" in the upper right corner of the popup to close the window (either by mistake or actually believing that the data is processed) and get a second dialog popup (you cannot change the text in this dialog) displaying "Are you sure you want to navigate away from this page?" - "Press OK to continue, or Cancel to stay on the current page" - OK/Cancel... Now, some of the users just press enter on their keyboard and oops, the popup is closed and all information is lost.
Question: Can you, or is there a workaround to set focus on "Cancel" instead of "OK" when it comes to the dialog of whether to close an Internet Explorer popup/tab or not?
Thank you in advance! =)
Unfortunately, I don't think it's possible to change focus on browser-generated dialog boxes.
As Deestan suggested, maybe you can create a faux popup using js and css, and do whatever you want in terms of styling it, and still have the user enter information, and have the information passed to your code.
From what i've heard/read, it's generally accepted that using alert() or popup() for anything other than debugging is frowned upon.

Popup block under FireFox

When I use JavaScript function
var w = window.open('about:blank');
under FireFox the popup is blocked and I get the option to unblock it. Even if I do this the popup does not appear (I believe the JavaScript needs to be fired one more time).
But I need to be sure that the popup fires even when the code runs for the first time.
So what I want is to check whether FireFox allows popups. There are two possibilities:
This should be done in such a way that if it does not allow then show appropriate message and wait (setTimeout?) until the access is granted (and open popup afterwards).
Before doing this window.open operation check if FireFox allows popups. If it does not, then show appropriate message (and do not allow the user to go deeper inside app without granting access) but if it does allow then do not open the popup (user does not need to see that he can fire popups). All of this can be done for example when user logs in (all of main popups require the user to be loged in).
So that's the idea. But what about JavaScript? How can I achieve this? Is it even possible?
When firefox blocks a popup, the reference to w will be null.
So my suggestion:
Try to open the popup
check if w is null
when w is null, show the message
put an element into the message with a notice like "click here to open the window"
observe the click-event of this element, when it fires, open the popup(firefox usually will not block a popup forced by a click-event) and hide the message
otherwise: hide the message after xxx seconds
Other ideas:
observe the click-event of the document and open the popup then when it's open yet(and remove the click-observation)
when the popup is null, use a modal-dialog that blocks the UI and give the user the option to open the popup via a click inside the dialog. I would prefer this, because here you have the chance to determine if the user want's to allow the popup.
don't use popups at all, when possible use inline-popups, e.g. a jquery-dialog, LightBoxes, etc.

Required fields not met when closing window

I'm looking for the best way to go about "forcing" the user to fill a textarea.
For my work we have a system that keeps track of time spent on a particular "task". Some tasks are required to have a comment while others are optional. At the top of the page there is a timer, a textarea for the comments and a list of different tasks.
So far I have it so when the user tries to stop the timer, it won't stop until the comment is written (if it is required). When the paged is closed while the timer is running, an onbeforeunload function sends an alert warning that the comments aren't filled out and then the "Are you sure you want to leave?" warning pops up.
As far as I can tell there is no way to prevent the user from completely exiting the page. The idea we had was when the user closes the window, have another simple page open that just has a textarea and an instruction telling the user to write a comment. I'm pretty new to JavaScript and web development so I'm not entirely sure the best way to go about this.
Put that text area in a popup or iframe or modal window where you can control its closing.
On these window.close you can call the functions to validate the text area is filled or not.
Am not sure you can put that in a popup or not .but thats the only good way i can think of !!
There is no way to prevent the user from leaving a page.
Built in pop up blockers will also block the system from opening up popup windows onunload. Only way to allow onunload popups is if your system admins can update every browser to add an exception to the browser security settings.
It is impossible to make a web application act like a client application.

JavaScript problem toolbar=no

I have a simple logon page. When the user is validated, the window navigates to a new page. The javascript is window.open('http://www.google.com',"mytest",'toolbar=no'); My expectation is that when it navigates away from our logon page and opens the google site that the back button would be disabled. But it's not. Does anyone have any idea why?
It depends on your browser. Ultimately, all you can do with javascript's window.open() is tell the browser what you'd like it to do, but it's not obligated to do it. Browsers can and do ignore some directives based on user preferences.
I believe the option your looking for is 'location=no', as that hides the address bar and therefore the back button too. The toolbar is things like favorites/etc.
This is bad practice - what happens if the user has javascript disabled? If the browser prevents the js from removing the toolbar of the main window?
Instead, amend the logon page to detect whether the user is logged in before showing the login form. If logged in, show a message saying so instead of the form - that way, a user clicking back won't be a problem.
I find it very annoying when a website messes around with my browser window, and generally don't come back.
This is what worked for me. Instead of disabling the back key. I listen for on unload event. I then write the following in javascript:
window.onbeforeunload = function () { return "You should not press the back button while in this application. If you continue, your work will not be saved and you will need to log back in."}
Java Script pops a dialogue box with OK and Cancel options. If the user clicks cancel. The application stays right where they are. The script is embedded within the tags. For me this is the ideal solution. I found this at
http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

Categories