Click here to Skip to main content
14,880,436 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys, i need a javascript code for my asp.net project,
1) on browser close current session expire(logout).
2)but not logout in postback , button click, hyperlink.
3)code support atleast 3 browsers google chrome, firefox, IE, b'coz my code not support in firefox.

plz help me

&
C#
lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="ManageNews.aspx.cs" Inherits="ManageNews" %>
<%@ Register TagPrefix="ucl" TagName="header" Src="~/header.ascx" %>
<%@ Register TagPrefix="ucl2" TagName="footer" Src="~/footer.ascx" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit.HTMLEditor" TagPrefix="HTMLEditor" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title>school</title>
     <script src="js/Scriptslogin/jquery.js" type="text/javascript"></script>  
    <link rel="stylesheet" href="css/jquery-ui.css" />
    <link rel="stylesheet" href="style.css" />
    <script type="text/javascript">
        var aClick = "false";
        var f5key = 116;
        var loc = window.location;
        $(document).bind('keydown', function (evt) {
            // Check for refresh
            if (evt.which == f5key) {
                //alert("116");
                aClick = "true";
                window.form1.submit();
            }
        });
   </script>
</head>
  <body onload="timer()"  önbeforeunload="fun('notpostback',event)">
    <form id="form1"  runat="server"  önsubmit="fun('postback',event)">
    <asp:ScriptManager ID="scriptmanager2" runat="server" EnablePageMethods="True">
      <ucl:header id="header"  runat="server">
    <div class="main-wrapper">
    <div class="container-fluid">
   <div class="row-fluid ">
   <div class="span12">
  <div class="primary-head">
<h3 class="page-header">News</h3>
</div>
<ul class="breadcrumb">
<li><a href="#" class="icon-home"></a>
 <span class="divider ">class="icon-angle-right"></span></li>
<li>Add<span class="divider">class="icon-angle-right"></span></li>
<li class="active">News</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="content-widgets light-gray">
<div class="widget-container">
 <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="form"><tr>
  <td width="10%" align="left" valign="top">
    News <font color="red">*</font>
           </td><td width="90%" align="left">
          <asp:TextBox ID="tbnews" runat="server" TextMode="MultiLine" Height="200px"
              AutoFocus="true" Width="95%" >
             <asp:RequiredFieldValidator ID="rfv_1" ControlToValidate="tbnews" runat="server" CssClass="errtxt" Display="Dynamic" ErrorMessage="Enter News " ValidationGroup="vg2" /></td> </tr>  <tr> <td colspan="2" align="right">
  <asp:Button ID="btnsubmit" runat="server" CssClass="new_sbtn" Text="Submit" ValidationGroup="vg2"  OnClick="btnsubmit_OnClick" />                                <asp:Button ID="btncancel" runat="server" CssClass="new_sbtn" Text="Cancel" ValidationGroup="vg2" OnClick="btncancel_OnClick" />  </td>  /tr>                        </tr></table></div>
					
</div>
</div>	</div>	<div class="row-fluid">	<div class="span12"><div class="<br" mode="hold" />"content-widgets">	<div>	<div class="widget-header-block"><h4 class="<br" mode="hold" />"widget-header">Manage News</div><div>  <asp:GridView ID="gv1" DataKeyNames="id" runat="server" AutoGenerateColumns="false"                           CssClass="grid_table2" BackColor="white" GridLines="both" CellPadding="0" CellSpacing="0"
                            Width="100%" OnRowCommand="gv1_OnRowCommand" OnRowDeleting="gv1_OnRowDeleting">
                            <HeaderStyle HorizontalAlign="Left" BackColor="#d4d4d4" Height="40" /> <alternatingrowstyle horizontalalign="Left" />                           <rowstyle horizontalalign="Left" />    <columns>
                                <asp:TemplateField HeaderText="News">
                                    <itemtemplate>
                                        <div><%#Eval("news")%></div>
                                    </itemtemplate>
                                
                                <asp:BoundField HeaderText="Date" DataField="datetime" />
                                <asp:TemplateField HeaderText="Action">
                                    <itemtemplate>
                                        <asp:LinkButton ID="lbedit" runat="server"   CssClass="icon-edit icon" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                                            CommandName="alter">
                                        <asp:LinkButton ID="lbdelete" runat="server"    CssClass="icon-trash icon" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                                            CommandName="delete">
                                    </itemtemplate>                         
                            </columns> </div></div></div></div>	</div>
</div>
	
    <div class="<br" mode="hold" />"scroll-top">	<a href="#" class="tip-top" title="Go Top">class="icon-double-angle-up"></a>
<ucl2:footer id="footer"  runat="server">
    </form>
       <script type="text/javascript">

           $(document).ready(function () {
               var Anchors = document.getElementsByTagName("a");
               for (var i = 0; i < Anchors.length; i++) {
                   if (($(Anchors[i]).attr("id") == "lbedit")) { }
                   else {
                       Anchors[i].onclick = function () {
                           aClick = "true";
                       }
                   }
               }
           });

           function fun(data, event) {
               if (data == "postback") {
                 //  alert("postback");
                   window. önbeforeunload = function () { }
//                   window.form1.submit();
               }
               else if (data == "notpostback") {
                   if (aClick == "false") {
                       ajaxCall();
                       alert("Logged Out");
                       return true;
                   }
               }
           }
           function ajaxCall() {
               jQuery.ajax({
                   url: 'ManageNews.aspx/Logout',
                   type: "POST",
                   data: "{}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: function (data) {
                       window.location.href = "Login";
                   }
               });
               return true;
           }
           function timer() {
               setTimeout(function () { ajaxCall(); }, 1140000);
           }
    </script>
</body>
</html>
Posted
Updated 28-Sep-13 22:08pm
v4

1 solution

You can check below mentioned link for more info:

<body onclick="clicked=true;" onunload="CheckBrowser()">
  

   <script type ="text/javascript" >
     var clicked = false;
     var xmlHttp
     var browser = navigator.appName;
     

     function CheckBrowser() {


         if (clicked == false) {
             xmlHttp = GetXmlHttpObject();
             xmlHttp.open("GET", "SessionEnd.aspx", true);
             xmlHttp.onreadystatechange = function() {
                 if (xmlHttp.readyState == 4) {
                     // alert(xmlhttp.responseText)
                 }
             }
             xmlHttp.send(null)
             if (browser == "Netscape")
             {
             window.location=".../SessionEnd.aspx";
                 alert("Browser Terminated");
                 openInNewWindow();
                }
         }
         else {
             //alert("Redirected");
             clicked = false;
         }
     }


     function GetXmlHttpObject() {
         var xmlHttp = null;
         try {
             // Firefox, Opera 8.0+, Safari
             xmlHttp = new XMLHttpRequest();
         }
         catch (e) {
             //Internet Explorer
             try {
                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
             }
             catch (e) {
                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
             }
         }
         return xmlHttp;
     }


     function openInNewWindow()
     {
         // Change "_blank" to something like "newWindow" to load all links in the same new window
         var newWindow = window.open(".../SessionEnd.aspx");
         newWindow.focus();
         return false;
     }
 

   </script>


You can have more solutions here :

http://forums.asp.net/t/1116238.aspx[^]

I hope this will help to you.
   

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