i have following code which return a popup message for reload page or not.
I have to perform a function when user click on "Reload page" and remain on same page when user click on "Not Reload".
window.onbeforeunload = function()
{
return "";
};
How can i do this?
Try with this:
<script>
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compatable
myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = ' '; // a space
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
</script>
Fetched from this article. This is their demo
Related
I am trying to warn the user before closing the tab. I search on many places for the right code but It seems that it doesn't work in chrome.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
In IE it works fine. I don't need to display any custom message. I just need to warn the user that they will leave the site. Any idea why this doesn't work? Is there any other way to force the browser to display warning before leaving site?
EDITED: I use Google Chrome version 74.0.3729.169
window.onbeforeunload = function (e) {
var message = "Your confirmation message goes here.",
e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}
// For Safari
return message;
};
Use the confirm() function:
function confirmExit(e) {
if (!confirm("You have attempted to leave this page. Are you sure?")) {
e.preventDefault();
return false;
}
}
I found the solution thanks to #barbsan. The link that he provided contains the solution.
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome etc.
});
https://developer.mozilla.org/en-US/docs/Web/API/BeforeUnloadEvent
We are using following code to show default pop up on refresh, tab close events
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
var confirmationMessage = 'You are attempting an assessment. Are you sure you want to leave this assessment?';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
}
});
I want to remove and unbind this event at runtime. I tried the following code but no luck.
$(window).off("beforeunload");
$(window).off("onbeforeunload");
window.onbeforeunload = null;
$(window).unbind("beforeunload");
Had to change the event binding code.
function closeIt() {
if ($('#timeExpiredtxt').hasClass('hide') && $("#submittedAnsResponseText").hasClass("hide")) {
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
}
window.onbeforeunload = closeIt;
and unbind used below code
window.onbeforeunload = null;
I have the following code:
<script>
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
myEvent(chkevent, function(e) {
var confirmationMessage = 'HELLO';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
</script>
Which runs a small pop up box when the user trys to refresh the page the script is running on.
However, how can I prevent it from running the pop up if they click the Submit button?
This should work. Note that you have to refresh the snippet for your message to appear, but not when you click the button.
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
var unloading = true;
myEvent(chkevent, function(e) {
if(!unloading) return;
var confirmationMessage = 'HELLO';
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
document.getElementById('submit').addEventListener('click', function(e){
unloading = false;
// Because this button does nothing, the reload will load an empty page in SO
// However, note there is no beforeunload event actioned.
location.reload();
});
document.write('Current Time: ' + Date.now())
<input type="button" value="SUBMIT" id="submit" />
I am trying to give alert to user if user click back button/press F5/ try to reload page.
I am using below script
<script type="text/javascript">
var testEvent = window.attachEvent || window.addEventListener;
var checkEvent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';
testEvent (checkEvent , function (e) {
console.log(e);
var hdnCountValue= $("#" + '<%=hdnCount.ClientID %>').val();
console.log(hdnCountValue);
if (hdnCountValue!= null && hdnCountValue!= "" && hdnCountValue!= 'undefined' && parseInt(hdnCountValue) > 0) {
var msg = 'Warning: You will lose all your data.';
(e || window.event).returnValue = msg ;
return msg;
}
});
</script>
It is working fine in Firefox and chrome.
But in IE9 & 10 it is working sometimes & sometimes not.
And also in safari it works but when user choose to stay on page it changes URL, it shows previous page URL.
We would like to throw a specific message before a user closes the browser. Is there a mechanism which will work across all major browsers (like IE, firefox, chrome, opera)
Check onbeforeunload:
window.onbeforeunload = function (e) {
var e = e || window.event;
if (has_message_to_throw) {
// For IE and Firefox
if (e) {
e.returnValue = 'Specific message';
}
// For Safari
return 'Specific message';
}
};
From Firefox Documentation
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome etc.
});