Check if page gets reloaded or refreshed in JavaScript

前端 未结 12 2874
遇见更好的自我
遇见更好的自我 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:37

    I found some information here Javascript Detecting Page Refresh . His first recommendation is using hidden fields, which tend to be stored through page refreshes.

    function checkRefresh() {
        if (document.refreshForm.visited.value == "") {
            // This is a fresh page load
            document.refreshForm.visited.value = "1";
            // You may want to add code here special for
            // fresh page loads
        } else {
            // This is a page refresh
            // Insert code here representing what to do on
            // a refresh
        }
    }
    
    
    
        

提交回复
热议问题