i have an issue regarding browser closing detection.i my asp.net application when user closes browser abruptlly i need to call a logout service which update loginstatus from true to false,i am doing this as below
function Logout() {
var Userid = $("#hdnfuid").val();
var branch = $("#hdnfbranch").val();
var service = LogoutService.Logout(Userid + "," + branch, SucceededCallback);
//alert(service);
}
function SucceededCallback(result) {
}
var isClose = false; ;
//this code will handle the F5 or Ctrl+F5 key
//need to handle more cases like ctrl+R whose codes are not listed here
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;
if (keycode == 116) {
isClose = true;
}
}
function somefunction() {
isClose = true;
}
function doUnload() {
if (!isClose) {
var selection = confirm('window is closing');
if (selection == true)
{var x=confirm("closing browser");
if(x==true)Logout();
else
return false;}
else {
return false;
}
}
but the problem is that when i click cancle of confirm box in that case also browser closes.
i also need to call same service on session_end event of global.asax
please suggest me the way of doing this.
}
but the problem is that
Related
Is there a better way to redo the following code below? A better approach? When the user hits the ESC key I need the page to redirect them back to their homepage (dashboard screen).
I'm having an issue with the Safari browser though. When a user in Safari hits the ESC key - my server is blocking their IP address (kinda like a brute force protection flag) and I think it's because it's trying to refresh the page over and over again without sending the person back to their dashboard.
The code below works great in Chrome and Firefox...
var keyPressed = {};
document.addEventListener('keydown', function(e) {
keyPressed[e.keyCode] = true;
}, false);
document.addEventListener('keyup', function(e) {
keyPressed[e.keyCode] = false;
}, false);
function goToControls() {
if (keyPressed["27"]) {
window.location.href = 'home.php';
}
setTimeout(goToControls, 5);
}
goToControls();
I used the following and it's seems to be happy now!
document.onkeydown = function(evt) {
evt = evt || window.event;
var isEscape = false;
if ("key" in evt) {
isEscape = (evt.key == "Escape" || evt.key == "Esc");
} else {
isEscape = (evt.keyCode == 27);
}
if (isEscape) {
alert("Escape");
}
};
HTML Button that I create
I wanted to know if there is something for an HTML button, simulate that the ENTER key was pressed. Since I need a button to send an automatic chat in which I am working. I was able to implement the button, although it still does not perform any action. The messages are sent by pressing the ENTER key, so I wanted to find a way to simulate that action. Thank you
var fncTxMessageKeydown = function (e) {
e = window.event || e;
var keyCode = (e.which) ? e.which : e.keyCode;
if (keyCode == 13 && !e.shiftKey) {
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
var message = elements.txMessage.value.toString().trim();
if (message!=""){
if (gotUid==true) {
EngtChat.sendMessage(message);
elements.txMessage.value = "";
}
else{
//show initialization alert and reconnect socket
document.getElementsByClassName("engt-sheet-header-with-presence")[0].style.padding="7px";
socket.disconnect();
socket.connect();
}
}
return false;
}
};
if (elements.txMessage != undefined) {
elements.txMessage.onkeydown = fncTxMessageKeydown;
As per the project requirement, on which I am working now, I need to show user a javascript alert only in the event of browser close using javascript. All other page events like url click, button click, F5 key press etc. are to be disregarded. I have tried with the following code but with no use.
var isPostBack = false;
$(function() {
// You would copy this for select and any other form elements I forgot about
$('input').live('click', function() { isPostBack = true; });
$('a').live('click', function() { isPostBack = true; });
document.onkeydown = function(e) { //attach to key down event to detect the F5 key
isPostBack = false;
if (!e) { //Firefox and Safari gets argument directly.
e = window.event;
}
var key = e.keyCode ? e.keyCode : e.which;
try {
if (key == 116) { //F5 Key detected
isPostBack = true;
}
}
catch (ex) { }
}
});
window.onbeforeunload = check;
function check() {
if (!isPostBack) {
// Do your unload code
isPostBack = false;
var strPath = window.location.pathname;
if (strPath.indexOf('CustomerPortal') >= 0) {
alert('Customer, you are leaving our page.');
}
else {
alert('User, you are leaving our page.');
}
return "Are you sure you want to exit this page?";
}
}
Please help to achieve my target requirement with your valuable comments and help.
Is it possible to trigger a key event only outside a form element?
Background: I have a code that loads the next page when the right key is pressed. But I don't want to trigger that event if somebody is using that key in a form element.
current code:
$(document).keydown(function(e){
if (e.keyCode == 37) {
var url = $('a#left').attr("href");
if (url != '') { // require a URL
window.location = url; // redirect
}
return false;
}
if (e.keyCode == 39) {
var url = $('a#right').attr("href");
if (url != '') { // require a URL
window.location = url; // redirect
}
return false;
}
});
If you have other fields outside your form this might be quite useful I hope
LIVE DEMO
document.onkeyup = function( ev ){
var key = ev.which || ev.keyCode ,
aID = { 37:"left", 39:"right" };
if( ! (/INPUT|TEXTAREA/i.test(ev.target)) && aID[key]) {
var url = document.getElementById( aID[key] ).getAttribute('href');
window.location = url;
}
};
Here is a basic example of the concept. Your simply adding an event handler to the document and checking that its target does not have a parent that is a form.
HTML
<form>
Inside form
<input/>
</form>
Outside form
<input />
Javascript
$(document).keyup(function(event){
if($(event.target).parents("form").length == 0){
alert("here");
}
});
Working POC: http://jsfiddle.net/48NYE/
This concept can be easily applied to the script you have provided.
Modification for your Script
$(document).keydown(function(e){
var outsideForm = $(e.target).parents("form").length == 0;
if (e.keyCode == 37 && outsideForm) {
var url = $('a#left').attr("href");
if (url != '') { // require a URL
window.location = url; // redirect
}
return false;
}
if (e.keyCode == 39 && outsideForm){
var url = $('a#right').attr("href");
if (url != '') { // require a URL
window.location = url; // redirect
}
return false;
}
});
I have a system where I want to check with the user if they're sure they want to leave the page once a dirty flag is set.
I'm using the following code - In FireFox, I can look at the page source through FireBug and the tag correctly has the onbeforeunload attribute inserted in it.
In Chrome and FireFox, this doesn't happen though and I'm able to navigate away from the page without any warning at all. The jQuery line to update the body tag is definitely being executed, it just isn't performing it.
if ($("body").attr('onbeforeunload') == null) {
if (window.event) {
// IE and Chrome use this
$("body").attr('onbeforeunload', 'CatchLeavePage(event)');
}
else {
// Firefox uses this
$("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
}
}
Any ideas how to proceed from here?
you cannot abort page unload by returning false. you must return string that will be shown to user in a message box, and he decides if he want to leave or stay on the page (by selecting either 'OK' or 'Cancel' button), so you need to write your code like this:
window.onbeforeunload = function() {
return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
};
try this
<script type=\"text/javascript\">
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'You sure you want to leave?'
function goodbye(e)
{
if(dont_confirm_leave!==1)
{
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation)
{
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
window.onbeforeunload=goodbye;
</script>
window.onbeforeunload = function () { return 'Are you sure?' };
Check this code :
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0;
var leave_message = "You sure you want to leave ?";
function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}
window.onbeforeunload = goodbye;
document.onkeydown = function () {
switch (event.keyCode || e.which) {
case 116 : //F5 button
validNavigation = true;
case 114 : //F5 button
validNavigation = true;
case 82 : //R button
if (event.ctrlKey) {
validNavigation = true;
}
case 13 : //Press enter
validNavigation = true;
}
}
// Attach the event click for all links in the page
$("a").bind("click", function () {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function () {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function () {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function () {
wireUpEvents();
});
It's not pretty, but it did the trick.
var warnclose = true;
var warn = function(e) {
var warning = 'Your warning message.';
if (warnclose) {
// Disables multiple calls
warnclose = false;
// In case we still need warn to be called again
setTimeout(function(){
warnclose = true;
}, 500);
return warning;
}
};
window.onbeforeunload = warn;