Skip to main content

Clear Session on browser/tab close using JQuery

Add below script to master page and replace Logon with your login page.
<script type="text/javascript">
var LoginPageName = "Logon";
var AttachClearSessionEvent = true; var IsFormSubmit = false;
$(window).submit(function () { AttachClearSessionEvent = false; IsFormSubmit = true; window.onbeforeunload = null; });
$(document).ready(function () {var myEvent = window.attachEvent || window.addEventListener;var chkevent = window.attachEvent ?
'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox
try {if (AttachClearSessionEvent) {var caller = '(document).ready';var urlToDisposeSession = LoginPageName;
$.ajax({cache: false,type: "POST",url: urlToDisposeSession,data: JSON.stringify({ RandomString: caller }),contentType:
"application/json; charset=utf-8",dataType: "json",async: false,success: function (msg) {}});}
}catch (ex) { }
});});
$(window).on('mouseover', (function () {AttachClearSessionEvent = false;window.onbeforeunload = null;}));
function addEvent(obj, evt, fn) {if (obj.addEventListener) {obj.addEventListener(evt, fn, false);}else if (obj.attachEvent)
{obj.attachEvent("on" + evt, fn);}}
addEvent(window, "load", function (e) {
addEvent(document, "mouseout", function (e) {e = e ? e : window.event;var from = e.relatedTarget || e.toElement;
if (!from || from.nodeName == "HTML") {
var callOnMouseOut = true;
if (IsFormSubmit) { callOnMouseOut = false; } if (callOnMouseOut) { AttachClearSessionEvent = false; window.onbeforeunload =
ConfirmLeavemouseout;}}
});});
function ConfirmLeave() {
try {var Caller = 'Keyboard';var urlToDisposeSession = LoginPageName;
$.ajax({cache: false,type: "POST",url: urlToDisposeSession,data: JSON.stringify({ RandomString: Caller }),contentType:
"application/json; charset=utf-8",dataType: "json",async: false});
}catch (ex) { }}
function ConfirmLeavemouseout() {
try {var Caller = 'mouseout';var urlToDisposeSession = LoginPageName;
$.ajax({cache: false,type: "POST",url: urlToDisposeSession,data: JSON.stringify({ RandomString: Caller }),contentType:
"application/json; charset=utf-8",dataType: "json",async: false,success: function(msg) {}});
}catch (ex) { }}
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" || prevKey == "CONTROL")) {window.onbeforeunload = ConfirmLeave;}
prevKey = e.key.toUpperCase(); });
</script>

Comments

Popular posts from this blog

Server Error (dots in URL)

Issue description: Asp.net Server error [Unable to redirect to custom error page] when three dots (…) placed after directory name in url, Custom error page will not work if below attribute is set to false. www.yourwebsite.com\somefolder\ ... By default in asp.net  application will be configured relaxedUrlToFileSystemMapping = " false, which means each http request will be validated by ‘Server OS file path validation component’. So when we pass (.) or (..) in URL first it will be validated by this component then the valid request will be passed to IIS. By setting relaxedUrlToFileSystemMapping = " true " it will bypass the ‘Server OS file path validation’, so all the http request will directly reach to IIS. Same scenario has been explained in below figure. Mitigation:                              ...

Visual Studio 2010 Error HRESULT E_FAIL has been returned from a call to a COM component.

I was using Visual Studio 2010.  I was debugging a web application and an exception happened and VS 2010 froze.  I ended the VS 2010 in the task manager and when I went back to developing, I found on every form for every ASP.net control I get:  Error Creating Control - Error HRESULT E_FAIL has been returned from a call to a COM component.  Also I am unable to edit  the form or add anything from the toolbox. Solution: This error comes because of Caching of Visual Studio Delete the Cache. You can delete the project cache at "Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache", then run "devenv /setup" to build the cache again to see if it helps.

Password Protected File Validation for(.doc/.docx/.xls/.xlsx/.pdf) file types

Password Protected File Validation for(.doc/.docx/.xls/.xlsx/.pdf) file types protected void btnUpload_Click( object sender, EventArgs e)         {             //Check if File Upload control has file or not             if (FileUpload1.HasFile)             {                 //Get Uploaded file bytes                 var bytes = FileUpload1.FileBytes;                 //Get Uploaded File Extension                 FileInfo objFileInfo = new FileInfo (FileUpload1.FileNam...