onbeforeunload auto click submit button - javascript

How to make it work, couse now it won't work. I want to auto click button if detect website close.
<script>
function myFunction() {
document.getElementById("makeOffline").click();
}
</script>
<body onbeforeunload="return myFunction()">

That will call your function. But the things your function is allowed to do, in a modern browser, are very limited. You can't, to pick a random example, submit a form.
In general, the only useful use case for onbeforeunload is to warn users that they may lose data if they leave the page, which you can do by returning a string from your handler. The browser will then check with the user to see if they really want to leave (most of them will show the string you return, but not all — Firefox doesn't anymore, for instance).
Anything else you might be trying to do in an onbeforeunload handler is almost certainly better suited to being done some other way. If you post a question about your end goal, people may be able to help you with it.

Related

Event on leaving the page

How can I write a function that will display an alert when the user tries to leave the page?
I've been thinking about a 1px high div at the very top of the website, and when the user hovers it an alert appears, but I can't place it there, the less html code I place for this purpose the better.
Another method I've been thinking of is unload, or beforeunload events, but they seem to be deprecated.
How can I do this?
How can I write a function that will display an alert when the user tries to leave the page?
The only way to do that is to use onbeforeunload, and you get very little control: Basically, you can supply a message which will probably (but only probably*) be shown to the user. The user will get two choices: Go ahead and leave, or stay.
onbeforeunload is a very unusual event. Here's how you hook it:
window.onbeforeunload = function() {
if (youWantToShowAMessage) {
return "The message goes here.";
}
};
Another method I've been thinking of is unload, or beforeunload events, but they seem to be deprecated.
I'm not aware of either of those being deprecated. What you can do in them is very restricted (for good reason), but until there's a replacement, onbeforeunload serves a useful purpose: It lets the web page tell the user they may be losing something (e.g., unsaved work) when they leave. The current mechanism which only allows a message and very, very few other actions is a decent one: It puts the user in control, but lets the page be useful.
* Mozilla has flirted with just showing a generic message, not showing your message.

Is there an event for the browser's back button being pressed?

I am supporting an e-commerce app, which pretty much makes and submits orders.
A user found that if they submit their order, and press back really quickly, they can cause an error condition.
I want to prevent this. When the user clicks submit, I want to bind some kind of event to the browser's back button that instead will redirect them to the Index page. However, after about two hours of Googling (including a few StackOverflow topics), I have not found any clear way of influencing the behavior of the back button.
I briefly attempted to use history.pushState(), but as the HTML 5 documentation mentions, that will not cause a redirect; it merely alters the displayed URL/state.
Similarly, the history.onpopstate event appears unhelpful, because it occurs whenever a state is removed from the history listing; I'm looking for an event that occurs whenever the history listing is traversed backwards.
Question: Does an event for the browser's back button, or at least a way to prevent this particular stupid user trick exist?
You can't listen to the browser back button because it's outside of your reach (it's not part of the DOM).
What you can do is fix the previous page so that it detects if you've used the back button.
Without more information I can't give you any tips on how to achieve that.
Also, an error condition is not necessarily a bad thing. Just make sure it's clear what is happening: the error message should make sense.
Wrong answer...
Instead listen to window.onBeforeUnload and ask the user if he knows what he is doing. Return false if not. This is usually done via a confirm dialogue

Usage of AJAX in an alert box

I have a javascript alert asking the user to insert a password. When user presses button, I will do an AJAX call to check the password. This could generate another alert with the response (if it was accepted or not), but I wonder if it is possible to have the AJAX response change the text of my first alert? Is it even possible to override the alert button's default not to close itself when clicking "okay"?
I think generally you should try and minimize the use of alerts in your web application. They are very obtrusive and annoying. Generally people just update the DOM to display user information. An alert is for something more unique and important than somebody just mistyping their password.
EDIT:
As other people are pointing out, you should use modal dialogs instead to have behavior as you specify.
I'm quite sure you can't do that since the way the alert is constructed and displayed is different in each browser (just look at mobile browsers). I think you'll safeguard your sanity longer if instead you use modal dialogs.
Being a blocking function, alert() is an abomination in itself, and owing to that would thwart the very thing you are trying to do.

Submit form when onUnload fired

Simple question (probably), but I'm buggered if I can find an answer...
I have a back-end CGI application and a number of dynamically-created webpages (plain ol' HTML+JS+CSS), each with a single form. In every form, in addition to a number of other submit buttons, there is a closeApplication button, which, when clicked, causes the page to be submitted and the CGI application to perform some cleanup processing.
Sometimes however, once thy've done their work, users being what they are (idiots), they don't bother to press the closeApplication button, and simply close the browser window.
I've been trying to figure out how to automatically 'fire' the closeApplication button if the user does this, by using the onUnload handler, but I can't seem to find out how to determine whether the user is closing the browser window/tab or simply submitting the page.
Is there a way within the onUnload handler to find this out?
p.s. I'm just using basic JS - no JQuery or other framework, but I doubt that matters...
Unfortunately, you're out of luck.
This question covers very similar ground: how to identify onbeforeunload was caused by clicking close button
What you could possibly do is store a variable when anything valid is clicked on your page that would navigate away, then check that in your onunload handler - if it's false (or whatever), then assume that they've closed the window and submit. I think this would probably be flaky though.

Generally valid way to execute a javascript only once (via cookies)?

I'd like to add an onbeforeunload javascript, asking the user to bookmark the page (there's a small button in the header for that purpose).
The problem is, no matter if they'd like to bookmark it, it's pointless and annoying after running once.
So, what's a generic solution to stop a javascript from running more than once?
Thanks,
Emilia.
EDIT:
Yes, I guess an onload event would be more appropriate?
I don't really want to add "big red buttons"...
Any basic example how a IP validation + script would look like?
I would say it's already a bad idea to use a pop up when the user wants to exit the page even if it is only once, it's annoying and obtrusive. I suggest you place a big button on site itself if you want to call the visitor to an action, bookmarking in this case.
If you still want to though, you should use IP validation and not cookies, cookies are temporal, they can be removed by the user, and visitors will not like to be presented the same suggestion over and over.

Categories

Resources