How to tell whether the page is refreshed or closed, background: Vue project, requirements: Close the page, next time jump to the landing page

Source: Internet
Author: User
Tags sessionstorage


Recent projects have this requirement: to close the window of the current system, log out,



Because if there is a security risk if you do not sign out, actually I want to say that the computer is okay don't lend someone alive to leave the station remember to lock screen, in fact, we set a cookie expiration time, after a period of time will automatically empty cookies.



After I check the information, I try various methods on the Internet, found that there are problems.



Summarize this attempt:






First, the page load only executes the onload, the page closes only executes the onunload, the page refreshes executes the onbeforeunload, then onunload, finally onload.



I have been proven to conclude that:





When the page loads only when the onload//page is refreshed, the onbeforeunload event is executed before the refresh, and the OnUnload event is the last onload event when the new page is about to replace the old page. When the page closes, onbeforeunload the event before onunload the event.



(2) for Firefox:



When the page refreshes, only the onunload is executed; When the page closes, only the onbeforeunload event is executed. So how do you tell if the browser is off or refreshing? I've been experimenting with all the times on the Internet.






Second, wit's judgment page refresh or close



Key: The OnLoad method is executed after the refresh completes, and the cookie is emptied according to the session flag variable.



My idea is: refresh and close, regardless of which browser will execute OnUnload method or onBeforeUnload method, in fact, we can not judge in these two methods, but after the refresh will be executed again the OnLoad method, So I put a flag-bit variable in the session in both methods. Then, when the OnLoad method executes, determine if there is a variable in the session, if so, the description is refresh, or it is closed.


Import Cookies from ' Js-cookie '/*distinguish shutdown and refresh, close exit login start*/window.onload=function(){ if(!window.sessionstorage["Tempflag"]) {Cookies.remove (' UserId ') Cookies.remove (' UserName ') location.reload (); //cannot save, force jump to landing page}Else{Window.sessionStorage.removeItem ("Tempflag"); }}window.onunload=function() {window.sessionstorage["Tempflag"] =true;} Window.onbeforeunload=function() {window.sessionstorage["Tempflag"] =true;}








The current project is used in a feasible, insufficient place: multiple pages of a system open at the same time, turn off one of the pages will also empty cookies, re-login



Who let the project log in with the cookie is stored, if replaced by the session storage login information is not the same





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.