Click here to Skip to main content
15,943,742 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.. Gud mrng.

I have d requirement that if the user clicked refresh button or f5 I need to redirect the page to Login page..

I had done with browser close using
$(window).bind('beforeunload', function (e) {
//my logiic
});

But i failed in the refresh or pressing f5..

if u know .
plz help me..

Thanks,
Honey..
Posted
Updated 30-Jul-17 10:21am

Link 1

Link 2

Check the above 2 links
 
Share this answer
 
check this[^]

Please check the above solution, it has used javascript, but i guess this would solve this problem of yours.
Thanks
:)
 
Share this answer
 
I got the solution by using below code...
I tested in IE,Chrome and FireFox...
It works great for me..

C#
@*//Script for detect Browser refresh..
<script type="text/javascript">

         var logoutFlag = true;
         var success = false;

         $(document).ready(function () {

             $("a,:input").click(function () {
                 logoutFlag = false;
             });


             var myEvent = window.attachEvent || window.addEventListener;
             var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';

             myEvent(chkevent, function (e) {

                 if (logoutFlag == true) {

                     $.ajax(
                     {
                         type: "POST",
                         traditional: true,
                         dataType: "json",
                         async: false,
                         url: '@Url.Action(UrActionName, UrControllerName)',


                         success: function (data) {

                             if (data == "Success") {
                                 success = true;
                             }
                         }
                     });

                 }
             });
           });
    </script>
 
Share this answer
 
v2
----------------html----------
JavaScript
<div id='iconos'></div>
<script>
refreshcontrol();
</script>

------------------JS-----------
JavaScript
function refreshcontrol(){
'use strict';
var keyCodes = {
  116 : "f5 "
  };
var body = document.querySelector('body');
body.onkeydown = function (e) {
  if ( !e.metaKey ) {
	if(e.keyCode==116)
	{
	var finsesion=1
$.post('finsesion.php',{action: "iconos", finsesion:finsesion},function(res){
	$('#iconos').html(res);
});
 e.preventDefault();
	}
}


-------------PHP finsesion.php-----------
<?php

session_destroy();
header('Location: login.php');
?>
 
Share this answer
 
Comments
Graeme_Grant 30-Jul-17 19:38pm    
Any reason that you are answering a 3+-year-old question that the OP has already accepted answers to? Please don't answer tombstoned questions! Only answer those that are current and requiring help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900