capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116
<p>i am creating an MVC application.There was a neccessitity to make a variable in a session to null upon closing of the application (i.e. window/tab) but not upon refreshing the application. 
I tried it through the following code.</p>

<pre><code>&lt;script type="text/javascript"&gt;
           window.onbeforeunload = function (e) {
               e = e || window.event;
               if (window.event.keyCode == 116) {
                   alert("f5 pressed");
               }
               else {
                   alert("Window closed");
                   //call my c# code to make my variable null, eg:Session["myVariable"] = null;
               }  
           };
&lt;/script&gt;
</code></pre>

<p>But when F5 is pressed then, "window.event.keyCode" is always 0 and not 116.
Because of which my variable is becoming null even upon F5 key press which is not my requirement.</p>

<p>Even when the application (i.e. webpage) is closed,even then its 0 (which is probably correct).</p>

<p>Please note that the above part of the code is in .cshtml file.</p>

<p>Can anyone tell where am i wrong ?</p>
