How to prevent logging out automatically when the back button is clicked? - javascript

I am using code to logout of application automatically when the browser is closed, but the impact of the code is when I press the browser's back button and navigate to another page then also it automatically logout from the application.
I want my code to logout automatically only when the browser is closed and not when I am navigating through back button
can you help me in this, Thanks
Here is my code
$(window).on('mouseover', (function () {
window.onbeforeunload = null;
}));
$(window).on('mouseout', (function () {
window.onbeforeunload = ConfirmLeave;
}));
function ConfirmLeave() {
$.get("call to a php code to logout");
}
var prevKey="";
$(document).keydown(function (e) {
if (e.key=="F5"){
window.onbeforeunload = ConfirmLeave;
}else if (e.key.toUpperCase() == "W" && prevKey == "CONTROL") {
window.onbeforeunload = ConfirmLeave;
}else if (e.key.toUpperCase() == "R" && prevKey == "CONTROL") {
window.onbeforeunload = ConfirmLeave;
}else if (e.key.toUpperCase() == "F4" && prevKey == "ALT") {
window.onbeforeunload = ConfirmLeave;
}
prevKey = e.key.toUpperCase();
});
The php code is
$this->session->sess_destroy();
$this->load->driver('cache');
$this->cache->clean();
ob_clean();
redirect("login page");

onbeforeunload event occurs when we close the browser and also when we press the back button. It fails to distinguish between the 2 actions.

Related

Call a function from JavaScript on clicking Reload button in a window before unload function?

I have a function in JavaScript which executes when the close or refresh button is clicked.
$(window).bind("beforeunload",function(event) {
return "Save or Not";
}
});
This will give me a pop up which has 2 buttons when I click on the refresh button (Reload and Don't reload in Chrome). Now I want to call a function to redirect to my homepage if I click on the reload button. How can I do that?
Please try this
$(document).ready(function() {
setTimeout(function() {
var __redirect_to = 'url_where_to_send';//
var _tags = ['button', 'input', 'a'], _els, _i, _i2;
for(_i in _tags) {
_els = document.getElementsByTagName(_tags[_i]);
for(_i2 in _els) {
if((_tags[_i] == 'input' && _els[_i2].type != 'button' && _els[_i2].type != 'submit' && _els[_i2].type != 'image') || _els[_i2].target == '_blank') continue;
_els[_i2].onclick = function() {window.onbeforeunload = function(){};}
}
}
window.onbeforeunload = function() {
setTimeout(function() {
window.onbeforeunload = function() {};
setTimeout(function() {
document.location.href = __redirect_to;
}, 500);
},5);
return 'Are you sure you want to leave this page?\n\nWait...Before you go:\n\n***************************************\n\n GET A FREE BOTTLE OF NeuroNZT\n\n***************************************\n\nTo receive your bottle FOR FREE - Click Cancel or Stay on Page below';
}
}, 500);
});
</script>

Safari trigger pop state twice and when page load

$(function ($) {
if (window.history && window.history.pushState) {
window.addEventListener("popstate", function (event) {
var hashLocation = location.hash;
var hashSplit = hashLocation.split("#!/");
var hashName = hashSplit[1];
if (hashName !== '') {
var hash = window.location.hash;
if (hash === '') {
var agree = confirm("Are you sure you want to leave?");
if (agree) {
'do something
} else {
'do something
}
}
}
});
window.history.pushState('forward', null, './Payment.aspx');
}
I would like to call the webmethod when user click browser's back button. These code working fine at all other browser except on Safari 5.1.7. The confirmation message should popup when user click browser back button but now popup when page load. Is it possible for me to used the current code to achieve this?

How to disable Warning by "onbeforeunload" when "reload" same page?

My code works well, but I do not need this "beforeunload warning" when reloadind same page (reload button ou F5 key) , or when a click in the "back" button...
My original working code:
<script>
window.onbeforeunload = function (e) {
var msg = '\n\n\nARE YOU SURE?\n\n\n';
e = e || window.event;
if (e)
e.returnValue = msg;
//some extra conditions
document.getElementById("popUpOut").style.display = 'block';
return msg;
}
</script>
So, this is my question: How to disable beforeunload in these situations ("back button" and "reload page)?
You can't do that. A page refresh is like navigating away and unloading the DOM so the onbeforeunload event will always be called but you can prevent it using jquery for keys pressed for Ctrl + R or F5 and Backspace.
For Ctrl + R use this:
$(document).keydown(function (e) {
if (e.keyCode == 65 && e.ctrlKey) {
e.preventDefault();
}
});
For F5 use this:
$(document).keydown(function (e) {
if (e.which || e.keyCode) == 116) {
e.preventDefault();
}
});
First of all, let me say that, ""I do not want"" stop reloading the page! BUT controll the beforeunload message!
So if you allow me, I will try to explain:
A) I just want do DECIDE in that event I show (or not) the "warning
exit message"
As far everyone say to me that, "it is impossible" do control UnbeforeUnload event, I make some tests and depending of the browser it is perfecty possible, BUT this is a "working progress rechearch"
So I know this:
1) listening the keyboard it's very easy to "chose the event" and,
what I want do show in the "warning mewssage" for each one.
2) Listening the history I can chose what happends on "Navigator's
Back button", dand do the same.
3) The code below works fine in chome...
And to control the keyboard, I have this, very simple code in JS:
document.onkeydown = KeyCheck;
function KeyCheck(e) {
var key = (window.event) ? event.keyCode : e.keyCode;
if(key==116) {flag_beforeunload=false;}
if(key==8) {flag_beforeunload=false;}
if (e.keyCode == 82 && e.ctrlKey) {flag_beforeunload=false;}
document.getElementById("container").innerHTML = "key = "+key + " - " + flag_beforeunload;
}
window.onbeforeunload = function(e){
var msg = 'You are sure to exit?';
e = e || window.event;
if(e)
if (flag_beforeunload == true) {
return msg;
}
}
Following, here (dotnsf site) is where I get the code for control the browser's "back button and"... I can even disable it.
but it is en Jquery, following is my code, but in JS:
window.onpopstate = function(event) {
window.history.back(-1)
if( !event.state ){
//the to lines below, disable the back button
// history.pushState( "nohb", null, "" );
// return;
// the following line iIcan use to control the envent, in UnBeforeUnload
// flag_beforeunload=false;
}
}
}
And finaly, this is my question:
I apreciate more help, and solutions for others navigators than Chrome!
Thanks a lot !

Hiding div with cookies after a second visit user

Hello friends have tried hard and not found how do I thank for the help now.
I want to hide a div with cookies after a second visit of the user that is when the User visit the page div appears later when the User leave the page and return the div does not appear.
Here my code:
jQuery(document).mousemove(function(e){
if( document.activeElement && document.activeElement.tagName == 'IFRAME' ){
jQuery.post(window.location.href, {click: 1});
document.getElementById('mime').remove();
}
});
jQuery(document).bind("contextmenu",function(e){jQuery("#mime").remove();});
document.onkeypress = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
jQuery("#mime").hide();
return false;
}
}
document.onmousedown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
jQuery("#mime").hide();
return false;
}
}
document.onkeydown = function (event) {
event = (event || window.event);
if (event.keyCode == 123) {
jQuery("#mime").hide();
return false;
}
}
<div id="mime" style="position:absolute; z-index:99999999999999; opacity:0.0; filter: alpha(opacity=0) ">
<script type='text/javascript'>
habilita=true;
if(document.all){}
else document.captureEvents(Event.MOUSEMOVE);document.onmousemove=mouse;function mouse(e)
{if(navigator.appName="Netscape"){xcurs=e.pageX;ycurs=e.pageY;}else{xcurs=event.clientX;ycurs=event.clientY;}
if(habilita){ document.getElementById("mime").style.left=(xcurs-230)+"px";document.getElementById("mime").style.top=(ycurs-150)+"px";}}
</script>
</div>
You can use js-cookie
Show modal only if modalShown value is undefined
if (!Cookies.get('modalShown')) {
// show modal
}
Save to cookies when user visits a page
Cookies.set('modalShown', true);

Trigger an event when the browser back button is clicked

I am trying to run some code when the browser back button is clicked.
How can i found out browser's back button with out changing the browser history?
I tried the code below.
I got an exception in the else block saying: "event is not defined".
window.onunload = HandleBackFunctionality();
function HandleBackFunctionality()
{
if(window.event)
{
if(window.event.clientX < 40 && window.event.clientY < 0)
{
alert("Browser back button is clicked…");
} else {
alert("Browser refresh button is clicked…");
}
} else {
if(event.currentTarget.performance.navigation.type == 1)
{
alert("Browser refresh button is clicked…");
}
if(event.currentTarget.performance.navigation.type == 2)
{
alert("Browser back button is clicked…");
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
use
$(window).on("navigate", function (event, data) {
var direction = data.state.direction;
if (direction == 'back') {
// do something
}
if (direction == 'forward') {
// do something else
}
});
Okay. Besides the fact that you should not initially trigger the event and to .unload = FunctionName and not .unload=FunctionName() and that you need to pass the event-argument I checked the code in the browser.
currentTarget is empty - this totally makes sense as there is no event-target like onclick but it is just the site reloading/unloading.
Please debug the code by yourself by using this and fit it to your needs:
window.onunload = HandleBackFunctionality;
function HandleBackFunctionality(event)
{
console.log(event, window.event);
}
You will see that currentTarget is not set (while event is).
This is the only solution that works for me with IOS safari.
<script>
window.addEventListener( "pageshow", function ( event ) {
var pagehistory = event.persisted ||
( typeof window.performance != "undefined" &&
window.performance.navigation.type === 2 );
if ( pagehistory ) {
// back button event - Do whatever.
}
});
</script>

Categories

Resources