I'm wondering if there is a way to submit the contents of a form when the user closes a tab/window. I've looked into the onunload and onbeforeunload events and think maybe this is the road to go down. I've tried
window.onbeforeunload = function(e) {
document.getElementById("my-form").submit();
};
but that doesn't work. Is there a way I can get this to work in the way I am describing?
If you can use JQuery, give this a try:
$(window).bind('beforeunload', function() {
$("#id-of-submit-button").click();
});
Related
I am now trying to detect only the event of the browser close button of IE11 in JavaScript.
I was able to detect the browser close button with the onbeforeunload event, but I also have trouble picking up other page transition events.
This code is I trying on it.
window.onbeforeunload = function(event) {
alert('Sure you want to close ?');
}
Is there any good way to do it ?
Try using the addEventListener syntax:
window.addEventListener('beforeunload', function(){
alert('Sure you want to close ?');
});
Actually the onbeforeunload Event will be fired when you close the browser/tab , refresh the page(or F5), submit a form, or jump to other pages..etc
AFAIK, there is not a perfect way to detect which operation exactly USER do.But for your question, i guess there are some html tag like
To ABC
that fired the onbeforeunload Event when they are clicked. If it is true, may be you can remove the Event when the HREF was active so that the Event would not be fired.
Try codes blow:
<script type="text/javascript">
$(function() {
$("a").click(function() {
window.onbeforeunload = null;
location.href = $(this).attr("href");
return false;
});
});
window.onbeforeunload = function(e) {
return 'Sure you want to close ?';
}
</script>
Note that this would not prevent the refresh opreation to fired the onbeforeunload Event, but i hope to provide some ideas for resolving your problem.
This code here works for a page refresh or closing the page. It pops up a dialog asking if you're sure you want to leave without saving.
$(window).on('beforeunload', function(){
return 'Did you save your changes?';
});
Trouble is I have a SPA and I'd like this to show on hashchange as well.
The alert works here but returning a string does nothing. I'm guessing that that dialog is
specific to the beforeunload event.
$(window).on('hashchange', function(){
alert("saved?");
return "this doesn't do anything";
});
How should I work around this?
I think you may have to capture click event on the hashes and cancel it if not confirmed:
$('a[href^="#"]').click(function(){
return confirm('Did you save your changes?')
});
Demo: http://jsfiddle.net/v8GbN/
I would like to use some kind of preventDefault function to overwrite what hitting the refresh button on the browser does (or also pressing CTRL/CMD+R).
Is there something that allows me to prevent refreshing the page?
I tried this but it doesn't seem to do anything in Firefox.
window.onunload = function(){
alert("unload event detected!");
}
You can use onbeforeunload to prompt whether they'd like to leave:
window.onbeforeunload = function() {
return "Are you really sure?\nI don't know why anyone would want to leave my beautiful website!";
};
However, you can't override it any more than that.
I have got a confirm dialog at the onunload event. It works well but there is only one problem: the window loses at onunload the focus and passes it automatically to the parent. But since there is still the confirm dialog going on I don't want to lose the focus on this window.
I have already tried to set the focus manually with window.focus() but this isn't working.
At the moment my code for the onunload event looks like this:
window.onunload = sessionConfirmation;
function sessionConfirmation(e) {
window.focus();
confirm('test');
}
Thank you in advance!
Not quite sure what your end-game is, but I'm assuming you want a confirmation dialog to show when the user tries to leave the page. If so, you want to do this instead:
window.onbeforeunload = sessionConfirmation;
function sessionConfirmation(e) {
return "test";
}
Hope that's what you were asking for!
I'm presently writing JavaScript, on clicking the close button in the window, I should get a confirm box. In the confirm box, I should display some message and there should be cancel and continue buttons.
On clicking cancel, the window should not be closed, but on pressing continue, the window should be redirected to another jsp.
Can someone please help me with this code? I tried using the custom confirm box, but that seems to return only a string and it cannot be used to redirect to a page.
This is impossible. You cannot redirect the user to another page when they close the window. This is for security reasons.
To display a confirm box when closing the window, you can use the onbeforeunload event. This will ask the user if they wish to leave the page or not. The confirm box is rendered by the browser, all you can customize on is the text.
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
When the user leaves the page, you can use the onunload event, but again, you cannot redirect them (you can make an AJAX call, but you cannot redirect the browser).
$(window).bind('unload', function(){
console.log('bye'); // Some browsers may block this.
// Chrome blocks alerts in this event.
});
check this example it may help you out
function setConfirmUnload(on){
window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage(){
return "Are you sure you want to leave this page";
}
setConfirmUnload(true) to enable the confirmation or false if you want to allow them to close the screen without a warning (if you have a close button for instance).
You can try also this (link : http://api.jquery.com/unload/)
$(window).unload(function(){
alert("Bye now!");
});
You need to bind a handler to the onBEFOREunload event, as none of the functionality you want will be possible with the onunload event (it is too late).
var myFunction = function () {
return "Are you sure you want to leave the page?";
};
window.onbeforeunload = myFunction;
As far as I know, there is no way to react to the user input as the confirm box is handled by the browser.