JavaScript code not working in Firefox for ASPX web site - javascript

I have the following JS code that helps signing out a session from an ASPX page so a 'log out' record can be stored in a database. The code works when the site is opened with IE11, EDGE, and Chrome. Using those three browsers the user gets the popup asking if staying on the site or leaving the site. When it comes to be tested in Firefox, at least version 54, it just does not work. Nothings pops up.
Code is:
//We want to capture when a user logs out from the system - this is a way to force the browser to log out the session
window.onbeforeunload = closingCode;
function closingCode() {
// when user clicks the 'X' from the browser, will be prompted to leave or to stay
//clicking any of the options will trigger an event that will capture when the session was logged out and saved in the db
var button = document.getElementById('btnLogout');
button.click();
return "You are closing or refreshing this site. Clicking 'Stay on this page' will log out your session. Clicking 'Leave this page' will close your window.";
//return null;
}
//this will prevent the popup to appear everytime user navigates between pages/links
$(document).ready(function () {
$('a[rel!=ext]').click(function () {
window.onbeforeunload = null;
});
$('form').submit(function () {
window.onbeforeunload = null;
});
});

Related

windows onbeforeunload doesn't work Javascript

I want to prevent a close tab or close browser in a web page.
I am using this code:
<script type="text/javascript">
window.onbeforeunload = function(e) { //Doesn't work well
return 'Exit';
};
$('#form').submit(function() { //No alert on submit form (this works)
window.onbeforeunload = null;
});
</script>
If I click somewhere in the page the code works fine and ask me if I really want to close the page, but if I reload the page and then I close the tab without click something in the page the tab close without show any alert.
Do you have some idea how to fix?

javascript open window url on exit

Hello I am trying to open a new URL on page exit in a browser window using javacript. My goal is when user closes the window to see the javascript alert box and when he press "Leave this Page" Instead ot the browser window to be closed to be redirected to google.com.
my code so far is the following:
<script type="text/javascript">
$(document).ready(function(){
var areYouReallySure = false;
var internalLink = false;
function areYouSure() {
if (!areYouReallySure && !internalLink) {
areYouReallySure = true;
location.href="http://www.google.com"
return "Do you wish to go to google?";
}
}
window.onbeforeunload = areYouSure;
$('a').click(function(){
internalLink = true;
});
});
</script>
Rigth now when I chose leave this page it only closes the window.
This is impossible, and with good reason. Imagine if spam sites controlled what happens when you close their page. As a security measure, the only thing you can do when a user tries to navigate away from your page is display a message. This was implemented mostly so users can be warned that they haven't saved their data.

In Mozilla Firefox browser, when we click on reload current page icon on the address bar, a blank page is shown

Our product supports IE and Mozilla Firefox, when I login to our Element Manager UI, in mozilla firefox, when clicked on the reload current page icon on address bar, I am redirected to a blank page. After that I have to click on adress tab and hit enter for reloading. Redirecting to blank page does not happen when I do F5 or click on address tab and hit enter, It happens only when I click on the reload current page icon on the address bar.
When I click on the reload current page icon, I get a pop up saying "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier." I then have to click Resend and then it takes me to blank page.
This happens only once, second time if I click on the relaod current page icon, I dont see the pop up and page reloads properly. Please help.
I have added parts of the code in my main.js
<head> <body onload="beforeStartDeviceManagement();" onbeforeunload="beforeCloseWindow();"> </head>
function beforeCloseWindow() {
// apply to 8000 or CB
if (theDevice.is8k != undefined || theDevice.isVSP != undefined) {
var vrfCheck = self.name.indexOf(vrfContextView.PrefixName);
if (vrfCheck >= 0) {
var idStr = self.name.substring(vrfCheck + vrfContextView.PrefixName.length);
var id = parseInt(idStr);
window.opener.closeVrfWindow(id);
} else {
if (window.isVrfWindowOpened()) {
window.closeVrfWindow(theView.vrfId);
}
if(window.helpPopup){
helpPopup.close();
}
if(window.theHelpWindow){
theHelpWindow.close();
}
// call save configugation function
theView.validateSaveConfig();
}
}
// else, do nothing
}

How to Fire Angularjs Event when close browser or window?

I want to clear local storage values when user close browser or window tab using in angularjs. I tried the following code.
window.addEventListener("beforeunload", function (e) {
var confirmationMessage = "\o/";
alert("exit");
(e || window.event).returnValue = confirmationMessage; //Gecko + IE
return confirmationMessage; //Webkit, Safari, Chrome
});
In the above code it asks the confirmation alert messages when refresh the page and also close the page. But i want to fire angularjs event when close the browser/Window Tab only no need to ask the confirmation messages.
In the last project I worked on, I used the following code:
$window.onbeforeunload = function (event) {
if (self.form.$dirty) {
return 'You have made changes, but you did not save them yet.\nLeaving the page will revert all changes.';
}
}
First it performs a check to see if the data in the form has been changed. If so, when the user tries to close the window or go to another url, a popup will be shown stating that there are unsaved changes.
So in this event you have access to the controller, so all angular events should be able to fire.
This one worked for me, but you need to pay attention that the custom message doesn't work in most of the browsers, (such as chrome, IE, firefox).
window.onbeforeunload = () => {
return 'Are you sure you want to leave without saving?';
}
This will fired when the user refresh or close the tab or window, with the browser default message.
i have tried this code, it works for me!
window.onbeforeunload = close;
function close(){
// do something...
localStorage.clear();
sessionStorage.clear();
return null;
}

java script+onbefore unload

i m creating a web application, after login user navigates to home page where i have a navbar(Links to pages/url).Navbar is like this
Home-Nvigates to home page
Profile-Navigates to profile page
Chat-navigates to chat page when user clicks on chat link .
I want that when user closes the browser window ,accidentally or manually he should get an alert for that i have used onbeforeunload function like this
<script language="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>
Now when i m closing the browser on chat page i m getting following alert "You have attempted to leave this page." that is working fine,but now the main problem is that when i m clicking on other links like
Profile or Home this function is called and me geting this alert even though i have implemented this code in chat.jsp .Even i m refreshing this page i m getting this alert mentioned above which i should get only when i closes widow of chat.jsp page rather than other pages or Refresh.Plese help
Onbeforeunload fires when the page is unloading be it by refresh, navigate or page close. You could add an onclick eventhandler to anchors on your page that remove your onbeforeunload event to avoid it and you might be able to do the same with an onkeydown event on the window/body/page however I don't believe there's a way to catch users pressing the browser's refresh button.
example:
<html>
<head>
<script type="text/javascript">
function warnUnload(){
return "Your message here...";
}
function linkNavi(){
window.onbeforeunload = null;
}
function isRefresh(e){
var key = null;
if(window.event) // IE
key = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
key = e.which;
if(key == 116) //F5
window.onbeforeunload = null;
}
window.onbeforeunload = warnUnload;
window.onkeydown = isRefresh;
</script>
</head>
<body>
Google
Home
</body>
</html>

Categories