I want when a user closes the tab or window or when he tries to move to another location different from my site to pops a confirm box, and if he confirm to execute an ajax script and then to close or change the window. I don't know how to do that. PS: I'm using jQuery.
$(window).unload(function() {
var answer=confirm("Are you sure you want to leave?");
if(answer){
//ajax call here
}
});
Just add your own alert/dialogue code to the function.
<script language="JavaScript">
function unload() {
alert('window closed');
}
window.onunload = unload;
</script>
Related
We are using jQuery v1.11.3
The requirement is to display a Confirmation dialog with Yes and No button. The Confirmation dialog needs to be displayed before closing the browser and when user click on close icon.
Below code tried to use but not working .
<script type="text/javascript">
$(document).ready(function(){
$(window).bind("beforeunload", function() {
return "Do you really want to close?";
});
});
window.onbeforeunload = function(e){
return 'Calling some alert messages here';
}
</script>
I have an anchor tag whose click event calls a JavaScript function, which opens a pop up window.
Now when the user clicks the close button of the pop up window I want to generate an alert(), which asks whether the user wants to close the pop up window or not
Using the below code the alert is generate in the parent window rather than the pop up
<script>
function openForm()
{
var new_window = window.open('file:///C:/Users/Tejora/Desktop/test.html','popup''width=825,height=585,scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=50,top=0');
new_window.onbeforeunload = function(){
alert("Are you sure you want to close the window"); }
}
</script>
Click
Thanks in advance...
When a string is returned from the onbeforeunload handler, the browser will display a confirmation dialog with that message. Replace this line:
alert("Are you sure you want to close the window");
With this:
return "Are you sure you want to close the window?";
Here is the relevant documentation.
I am trying to display a confirmation message when the user closes the browser/tab and not when any of the links on page is clicked.
I have got the first part of displaying the message on window close working nicely but problem is that the message/alert is displayed also when user clicks on any link on the page.
I have got code from somewhere to prevent this behavior but still when ever any link is clicked the alert pops up.
Here is the code:
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/backfix-min.js"></script>
$(document).ready(function() {
$(window).bind('beforeunload', function(e){
$("#lead-gen-modal").dialog("open");
// This line only appears in alert boxes for IE
return "Wait\! Don\'t Go\! We have a special offer for you\. Stay on this page to receive big savings\!";
});
$("a").click(function() {
window.onbeforeunload=null;
});
});
just use a global variable
and set it to false when clicking a link.
var key = true;
$(window).bind('beforeunload', function(e){
if(key)
$("#lead-gen-modal").dialog("open");
});
update :
$(document).on("click","a",function() {
key=false;
});
or if you just want to prevent closing window you can do this :
window.onbeforeunload = function(e){
if(key)
return false;
}
I think you are looking for something like this.
$(window).unload(function() {
alert("bye");
});
If that does not work on Chrome try this
$(window).on('beforeunload', function() {
return "bye";
});
jQuery API: unload() http://api.jquery.com/unload/
I have the following code
<script type="text/javascript">
function PopIt() { return 'Are you sure you want to leave?'; }
function UnPopIt() { /* nothing to return */ }
$(document).ready(function() {
window.onbeforeunload = PopIt;
$('a').click(function(){ window.onbeforeunload = UnPopIt; });
});
</script>
This script works. But how do I alter it so it works like this;
1) User presses exit tab/page
2) page changes to one of my choice
3) exit popup displays with yes or no to leave
4) yes = close page, no = stay on current page
I'd like the page to change before the popup displays
Thanks.
Note: I do have control over the pages, I wish to redirect to another .php in the same folder.
Try using both onbeforeunload and onunload together like this...
function PopIt() {
return 'Are you sure you want to leave?';
}
function UnloadIt() {
window.opener.nowDoThisOpener("pass this variable along too");
}
$(document).ready(function() {
//set the function defining what should be done BEFORE unloading
window.onbeforeunload = PopIt;
//set the function defining what should be done ON unloading
window.onunload = UnloadIt;
//set all links to disable both of these on click
$('a').click(function(){
window.onbeforeunload = null;
window.onunload=null;
});
});
Note that nowDoThisOpener is a function that you can define (And obviously call whatever you want) on the parent page. And, like I've suggested, you can pass along information too.
Also, in your example you were setting an empty function UnPopIt to cancel the onbeforeunload. That's unnecessary, you can just set the onbeforeunload to null, as well as the onunload, as I've done in my example.
Previous Answer:
Could you put some kind of flag in the hash when you redirect? So instead of sending off to http://www.pageofmy.com/choice.php you sent to http://www.pageofmy.com/choice.php#1
Then on choice.php you could have...
<script>
if (location.hash=="#1") {
//show alert
}
</script>
This assumes that you have control over pageofmy.com/choice.php. If you're redirecting to some other site you don't have control over, I don't see how you can do this besides attempting to have a popup window come up (which will most likely be blocked by modern browsers)
What I am trying to do is make a pop-up box any time a page exits or is navigated away from. Right now I have
<script type="text/javascript">
function box()
{
var r=confirm("Message");
if (r==true)
{
window.location.href="yes.html";
}
else
{
window.location.href="no.html";
}
}
</script>
<body onunload="box();">
I have 2 problems with this:
It only shows the box if you actually navigate away from the page, refresh, new url etc. If you exit the tab or browser, the box doesnt pop up.
No matter what button you press, it just sends you where you tried to go originally, it never sends you to no.html or yes.html.
Could someone tell me how this is possible?
Try this:
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
setTimeout(function() {
setTimeout(function() {
window.location.href="yes.html";
}, 1000);
},1);
return "Message";
}
</script>
You can only catch the stay on the page option, you cannot override a user leaving the page. similar to: Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?