I am using the following code to prompt the user that if he left the page he can't come back. Things happened that i want to redirect the page using setTimeout function after a specific time. I want to disable checking for user leaving the page when the automatic redirection starts.
<script language="javascript" type="text/javascript">
needToConfirm = true;
window.onbeforeunload = askConfirm;
function askConfirm(){
if (needToConfirm){
return "Please note that you might not be able to come back and watch the movie again.";
}
}
</script>
Unset onbeforeunload in the function called by setTimeout before you redirect the user:
window.onbeforeunload = null;
Related
I need do a automatic foward/redirect.
If the user dont click anywhere in the site five seconds after automatic foward to homepage... it is possible?
For example,
<meta http-equiv="Refresh" content="5;URL=http://www.teste.com/sv1/index.html">
Thanks
try this in your head:
<script type="text/javascript">
var redirect = setTimeout(function() {
window.location = "http://stackoverflow.com";
}, 5000);
document.onclick = function() {
clearTimeout(redirect);
}
</script>
In PHP it's not possible. You can add timeout in JS and stop it when user click somewhere (onclick event on body).
After that timeout (without clicks), yuo can redirect user by setting document.location.href to your homepage.
Short: No.
Longer: This is not possible with PHP, because PHP is precompiled on the server. So as soon as the user sees the page on his browser, the PHP script already ran through. You will have to use something else instead, for example JavaScript.
I need to prompt a user when they are leaving my ASP.Net page unexpectedly with a message to ask if they are sure they want to leave. A post back or when the save button is clicked should not fire the warning. There are a bunch of articles covering this but I am brand new to this and appear to have got my wires crossed.
The recommended way appears to be to use the window.onbeforeunload event but behaves unexpectedly for me. This is fired when the page loads as opposed to when the page unloads.
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>
If I use the JQuery implementation it fires when the page unloads but the problem is it fires before the code behind is executed. So I cannot set a variable on the client saying don’t fire the event this time as it is a post back or a Save.
$(window).bind('beforeunload', function () {
return 'Are you sure you want to leave?';
});
Can anyone point me in the correct direction as I know I am making basic mistakes/miss-understanding?
Edit:
So I am nearly there:
var prompt = true;
$('a').live('click', function () {
//if click does not require a prompt set to false
prompt = false;
});
$(window).bind("beforeunload", function () {
if (prompt) {
//reset our prompt variable
prompt = false;
//prompt
return true;
}
})
Except the problem is in the above code I need to be able to differentiate between the clicks but I haven't been able to figure that out yet i.e. I am missing a condition here "//if click does not require a prompt set to false".
Any ideas?
Thanks,
Michael
You can try using this:
$(window).unload(function(){
alert('Message');
});
In case people are interested this is the roundabout solution to my problem. How to tell if a page unload in ASP is a PostBack
I have a function like this to prompt the user to confirm they want to navigate away from the page:
window.onbeforeunload = function() {
return "You may lose any unsaved changes.";
};
Now I'd like to check whether this is a just a page refresh (F5), and not render the message in this case. Is this possible?
Thanks
Nope, that's not possible, there is no way to find out if the refresh button of browser was pressed.
I know somethings like this;
<script>
function out() {
alert('No, stay here.');
//What to do?...
}
</script>
<body onUnload="out()">
<input />
</body>
I got some response from other community
<script>
function out() {
alert('No, stay here.');
location.href='thisPage.html';
}
But this will reload the page. I want to prevent users from moving pages without reloading page. Thank you
You can use the onbeforeunload event and take the steps whatever you want:
window.onbeforeunload = function(){
// whatever you want to do now
};
With this, you can redirect users to some other page if they try to move away from current page or any action you want to perform there.
Don't ever do this: there's a reason a user wants to leave the page, so don't force him/her to stay. A better option would be to use onbeforeunload to ask for confirmation before leaving the page.
Try return false; instead of location.href='thisPage.html';
Instead of returning nothing, add
return false;
to the end, that should work
:)
function out() {
alert('No, stay here.');
return false;
}
Bascially I have a page for editing Events that needs to warn a user of unsaved changes when moving page. If the server throws an error I use Seams redirect filter to go to error.xhtml, however this pops up the confirm dialog and allows users to cancel the page redirect and remain on the broken editing page.
Can I change the below code to pull out where I'm being directed to? I've tried window.location but that still says the Event page. Is there something like a window.redirect?
<script language="JavaScript" >
window.onbeforeunload = confirmExit;
function confirmExit(){
if(isEventModified()){return "The Event has unsaved changes!";}
}
</script>
You would usually set a global flag like
warn_on_exit = true;
if the flag is true, you return your warning string, causing the confirmation message to pop up. If it's false, you return null.
Your error page would then have to emit some JavaScript that sets the flag to false.