Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:18:36+00:00 2026-06-18T07:18:36+00:00

i am creating an MVC application.There was a neccessitity to make a variable in

  • 0

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.

<script type="text/javascript">
           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;
               }  
           };
</script>

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.

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

Please note that the above part of the code is in .cshtml file.

Can anyone tell where am i wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:18:37+00:00Added an answer on June 18, 2026 at 7:18 am

    You have to listen to different events if you want this to work crossborwser + you have to listen to the key-event every time its pressed, not on load:

    document.onkeydown = fkey;
    document.onkeypress = fkey
    document.onkeyup = fkey;
    
    var wasPressed = false;
    
    function fkey(e){
            e = e || window.event;
           if( wasPressed ) return; 
    
            if (e.keyCode == 116) {
                 alert("f5 pressed");
                wasPressed = true;
            }else {
                alert("Window closed");
            }
     }
    

    here is a demo: http://jsfiddle.net/FSrgV/1/embedded/result/

    but if you simply want to know if the user quits the page you could simply use window.onbeforeunload: https://developer.mozilla.org/en-US/docs/DOM/window.onbeforeunload

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe not specific to reports but still... In my asp.net mvc web application there's
I'm creating a PHP web application framework (MVC). I'd rather not use external libraries
Upon creating a new ASP.NET MVC 3 Razor site, there are 9 Account\*.cshtml files
I am creating an asp.net mvc application, where there is a text box where
I'm currently creating an MVC application that will likely to expand to include a
I'm creating an MVC application that's following a RESTful URL approach. I am experiencing
I am working on creating an MVC application for an existing Bug tracker of
I am creating a Spring MVC Hibernate application using MySQL. Where should I save
I am creating a asp.net mvc application and loaded the data into jqgrid and
I am creating an ASP.NET MVC application that has postcode lookup functionality. I capture

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.