Handle refresh page event with JavaScript - javascript

Is it possible to use JavaScript to handle the event of refreshing page?
What I want is get notice if user do one of these behaviours:
refresh page by pressing F5
close tab or browser
enter a new url then press enter on
browser
to display a warning message?

You don't want the refresh, you want the onbeforeunload event.
http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
Sample code from article
<HTML>
<head>
<script>
function closeIt()
{
return "Any string value here forces a dialog box to \n" +
"appear before closing the window.";
}
window.onbeforeunload = closeIt;
</script>
</head>
<body>
<a href="http://www.microsoft.com">Click here to navigate to
www.microsoft.com</a>
</body>
</html>

The closest you could get is the window.onbeforeunload event:
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = 'Leaving the page';
}
// For Safari
return 'Leaving the page';
};
It is important to note that you need to return a string from this function.

Related

show alert message when closing a window? Not Working

I want to show alert before closing a prticluar tab. I have tried different codes over here but couldn't suceeded somwhow.. could somebody guide me where I am going wrong..
My Last try was :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" />
</head>
<body>
<script>
window.onbeforeunload = function (event) {
var message = 'Sure you want to leave?';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message;
}
</script>
</body>
</html>
Depending on your browser your code works. I've updated the snippet below so it always sets a returnValue which is required by some browsers. If you click the Run code snippet button below and attempt to close the tab you'll get an alert asking if you're sure you want to leave.
However what's important to know is that most browsers have removed the ability to serve custom messages on these alerts for security purposes.
window.onbeforeunload = function (event) {
event.preventDefault();
event.returnValue = '';
};

java script code for preventing user from closing browser without alert messege in chrome

This code is giving alert message but I want to prevent user from closing browser. If I comment this line and place alert code before below given code it works, but I want not to show any dialog box.
set Interval(function () {alert("**Hello")}, 10);
I tried above code but I close browser from current tab also:
{
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
confirmExit();
}
Try this Code..
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
Here is the Fiddle try this also.....

how to use window.onbeforeunload?

I have a small webpage. i need to activate my ajax function when browser is closed
I using below JavaScript for that in the <head>
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
my ajax_function();
return "";
}
</script>
But this function works all in the Back button, Forward button, ALT+f5, CTL+f5, Submit etc... But i need this function only when browser is closed
But i try like below in my webpage to block the events like Back button, Forward button, ALT+f5, CTL+f5, Submit etc...
<script type="text/javascript">
$(function () {
$('a').click(function(){window.onbeforeunload = null;});
$(window).keydown(function(event) {
if (event.keyCode == 116) { // User presses F5 to refresh
window.onbeforeunload = null;
}});
$('form').submit(function() {
window.onbeforeunload = null;
});
});
</script>
But this only for hyperlink, refresh, and submit.
What i need is, window.onbeforeunload function only works when browser is closed
JavaScript doesn't distinguish between these events, they are all triggered because the user is leaving the page.

prevent Page refresh after pop-up

How can i prevent page refresh when the user clicks on the OK button of the javascript alert window.
<asp:Button ID="btnComplete" runat="server" Text="Complete" OnClientClick ="verify_complete()" />
function verify_complete() {
if (!chkCmnts()) {
alert("Categories/comments are required. ");
return false;
}
}
Thanks
BB
You are already returning false from the JS so you need to change your markup to:
<asp:Button ID="btnComplete" runat="server" Text="Complete" OnClientClick ="return verify_complete()" />
If the verify_complete() function returns true it will post back, if it doesn't, it won't.
You can not prevent this, but you can notify the user via a popup that he should not reload. This is done with the onbeforeunload window event, best documented at MDN.
add this to your popup window in a Script-Tag
var controllVar = 1;
add this to your parent window. (popup is the return of window.open(...)). Here is actually some intensive error testing, because IE in it's various flavours shows more than one Bug.
window.onbeforeunload = function (e) {
//here we try accessing the popup
try {
if(!popup && popup.controllVar != 1) {
return;
}
} catch(e) {
return;
}
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'YOUR ERROR MESSAGE';
}
// For Safari
return 'YOUR ERROR MESSAGE';
};
This will behave exactly like here on Stackoverflow, if you add some text to the answer textarea and than try to leave the page.

How to capture browser close event?

I want to capture the browser close event in my application and show a confirm box to user.
I am using JSF 2.0 and richfaces 4.0.
window.onbeforeunload = function ()
{
var shallIAlertUser = Do_Whatever(); //get boolen value
if (shallIAlertUser) {
//this will alert user
return 'Are you sure?';
}
else {
//this wont
window.onbeforeunload = undefined;
}
};
Use the beforeunload event.
window.onbeforeunload = function(event) {
event = event || window.event;
var confirmClose = 'Are you sure?';
// For IE and Firefox prior to version 4
if (event) {
event.returnValue = confirmClose;
}
// For Safari
return confirmClose;
}
Keep in mind this will be fire for other events besides closing the window, such as reloading and form submission.
onbeforeunload
< body onbeforeunload="alert('Closing');">
Example :
<html>
<head>
<title>`onbeforeunload` Event Demo</title>
</head>
<body onbeforeunload="return 'Are you sure you want to exit ?';">
</body>
</html>
Attach a handler to the unload event.

Categories

Resources