Check if page gets reloaded or refreshed in JavaScript

前端 未结 12 2880
遇见更好的自我
遇见更好的自我 2020-11-21 22:57

I want to check when someone tries to refresh a page.

For example, when I open a page nothing happens but when I refresh the page it should display an alert.

12条回答
  •  误落风尘
    2020-11-21 23:23

    Store a cookie the first time someone visits the page. On refresh check if your cookie exists and if it does, alert.

    function checkFirstVisit() {
      if(document.cookie.indexOf('mycookie')==-1) {
        // cookie doesn't exist, create it now
        document.cookie = 'mycookie=1';
      }
      else {
        // not first visit, so alert
        alert('You refreshed!');
      }
    }
    

    and in your body tag:

    
    

提交回复
热议问题