window.onunload event not firing - javascript

I have a silverlight application in which I need to fire signout event if user navigates away.
I have tried a sample function as
window.onunload = function test() { alert("test"); }
The problem is when I navigate away from the webpage the event fires, but I need this event to fire when user closed browser or tab.

In my case it was not necessary that the page prompts user if he is sure he wants to exit. So I called my signout method inside the 'window.onbeforeunload' event(also flashed a message box to so that webservice gets enough time to successfully signout).
Also used
document.onbeforeunload = undefined;
inside my 'window.onbeforeunload' event to avoid the confirmation window.

Related

Use beforeunload only on tab close

below code will trigger a popup with Reload site? or Leave site? message
on page refresh, press back button or on tab close
How to trigger this event only on tab close
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault();
// Chrome requires returnValue to be set
e.returnValue = '';
});
There is no direct way of doing it because javascript doesen't have access to read browser actions. But if you want to do something on the server based on these actions, you may find this helpful.

.beforeunload only working on refresh

I am trying to alert users before they go to another page within the app, if there is any unsaved information. I'm following the advice from several stackoverflows to use .beforeunload
but it is only working when I refresh the page, and not if I click on a link to another page on the site. Am I misunderstanding how to use beforeunload or am I needing another event listener?
application.js
//= require forms.js
forms.js
$(window).on('beforeunload', function () {
console.log('unloading')
});
The beforeunload event works like this:
// Fires just before leaving/refreshing the page. Handler will
// be passed the event object and if a returnValue is set on the
// event, the browser will display a confirmation dialog with the
// returnValue message displayed.
window.addEventListener("beforeunload", function (evt) {
evt.returnValue = "Setting this to anything but an empty string, causes this to work!";
}, false);
See this Fiddle for an example.

Click event in the modal window doesn't get fired

In my application, when the session gets time out a modal window asking for a password gets dispalyed. When I click the submit button, the event doesn't get fired and It again triggers the session call.The screen gets properly redirected but the thing is I can't invoke any javascript function from the modal window. What will be the problem?
most likely due to the fact that when you dynamically add an element you have to assign click event like this:
$("h2").on('click', 'p.test', function() {
alert('you clicked a p.test element');
});
Need to see your code but its what i ran into a while back. good luck.

Ionic event listener fires on every screen, only want is to apply to one controller

in my ionic application I am binding my using the following code to implement a device back button:
var deregister = $ionicPlatform.onHardwareBackButton(
function () {
toastr.warning("Back button pressed");
}, 100
);
$scope.$on('$pause', deregister);
The function executes when I tap on the back button, but it executes on every other screen as well. I only want it to apply to the controller I am using it in. Is there any way that I can accomplish this? Thank you in advance.
EDIT
The reason that I am trying to implement this is that after the application is installed the first time, my device back button event does not fire if I want to exit the application. Splash page is displayed, login screen shown, then with correct credentials home screen is shown. I clear the history on login and home. On home the back button does not work. When I add the back button event listener, the event is applied on all the screens, not just the controller I use this code in. That is the problem.
var deregister = $ionicPlatform.onHardwareBackButton(
function () {
toastr.warning("Back button pressed");
$scope.$$listeners.$pause=[];
}, 100
);
$scope.$on('$pause', deregister);
Untested code and I'm not sure if it fits your scenario, but you can unbind the event in the event handler so that it only gets executed once?

Different actions with onbeforeunload

I am working on a simple chat script using Ajax and want to indicate when a user leaves the page. Have read several docs and found this works:
window.onbeforeunload = leaveChat;
function leaveChat(){
... my code
return 'Dont go...';
}
Unfortunately (and logically), if they cancel the exit, my code is still executed and they are flagged as leaving even though they are still on the page? It should only execute if the confirm leaving the page. Any suggestions?
I would use onunload, but it doesn't seem to work in any of my browsers (Chrome, IE).
First, you should add the event handler using:
window.addEventListener('beforeunload', function() {
// Confirmation code here
});
window.addEventListener('unload', function() {
// fire pixel tag to exit chat on server here
// UI interactions are not possible in this event
});
For further research:
unload event reference
beforeunload event reference
Window.onunload reference

Categories

Resources