alerting users before they leave page if they filled some fields - javascript

I want to use two javascript events: onbeforeunload and inside it I need to use onhashchange. The point is on leaving the page if user has filled some fields then the confirmation message appear warning user whether they want to stay on page or leave.
My code is following
window.onbeforeunload = function (e) {
window.onhashchange = function (r) { return ''; }
};
But the above code does not seem to be working? what am I doing wrong?
[Updated]
Again, what I am trying to achieve is I want to alert a user if the leave the page and if they have filled some fields on the page

You can try
window.onbeforeunload = function (e) {
if (content on page changed) { confirm("Do you want to leave page?") }
};
window.onhashchange = function (r) { if (content on page changed) { confirm("Do you want to leave page?") } }
I have just given pseudo code, you can replace with actual code.
Basic mistake is you are trying to bind event onhashchange in onbeforeunload.

Related

Dirty form check - beforeunload confirmation not working

I am attempting to simply capture when my form is dirty and if it has been changed, alert the user before they leave the page without saving.
Here is my current code:
<script>
var form = $('#MyForm'),
originalForm = form.serialize()
$(window).bind('beforeunload', function () {
if (form.serialize() != originalForm) {
return 'You have unsaved changes';
}
});
</script>
Nothing happens above. I can use beforeunload method just fine until I attempt to add the "dirty" field check, then nothing occurs.
Any help?

Is It possible to call a user defined JS function when the page refresh button is clicked?

Is It possible to call a user defined JS function when the page refresh button is clicked? I want to call a reload function, if yes is clicked I want the user to logout.
Below is my code
I used. but am getting a different confirmation pop up which reloads the page. I am nor able to call my custom function. Below is my code window.onbeforeunload = function() {
var r=confirm("Do you want to leave page!");
if (r)
{
//write redirection code
sessLogOut();
}
else
{
//do nothing
}
};
A good solution for you may be using this in your html:
<body onunload="pageUnload()">
and this in JS:
function pageUnload(){
if(confirm("Do you want to leave page!")){
// DO SOMETHING
} else {
// DO NOTHING
}
}

How to Disable Are you sure you want to navigate away from this page? messagebox in onbeforeunload

I use the following onbeforeunload for call when the browser close.I get the alert properly also i get the Are you sure you want to navigate away from this page? message box automatically generate by the web browser.But I no need to display that message box when the browser is close.How to disable Are you sure you want to navigate away from this page? messagebox?
window.onbeforeunload = function(event)
{
if($.browser.msie)
{
alert('Test');
return false;
}
}
Don't return a value, ie:
return;
Or, since the condition is a constant factor:
if($.browser.msie) {
window.onload = function() {alert('Test');};
} else {
window.onbeforeunload = function(event) {
return 'Are you sure to quit?'; // <-- Message is displayed in *some* browsers
}
}
If you're trying to implement a method which prevents the user from navigating away: There is no way to achieve that. It'd be a horrible experience to not be able to leave a page.

How to capture when a user is leaving ASP.Net page unexpectedly

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

Intercept selection in window.onbeforeunload dialog

in my web application if the user leaves the current page without having saved changes in the form a pop up window is opened to alert him about that.
For the pop up I use some scripts code injected from codebehind (C#):
var Confirm = true;
window.onbeforeunload = confirmClose;
function confirmClose()
{
if (!Confirm) return;
if(/*CHECK CHANGE CONDITION IS TRUE*/)
{ return " + WARN_message + "; }
}
I would need to intercept whether the user click on cancel or ok button.
I tried like:
var button_pressed = window.onbeforeunload = confirmClose;
But it returns always true.
How can get which button was pressed?
Thanks
Not possible. There is no event associated with the buttons.
What you might be able to do was to see if the user came back by setting a value or perhaps a cookie in the page in the onbeforeunload and test if it is there after some time has passed
but see the duplicate Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

Categories

Resources