Js判断页面是首次加载还是二次载入刷新

1 利用window.name属性在页面刷新时不会重置判断(在该属性空置的情况下可使用)

    if(window.name == ""){
    	  console.log("首次被加载");
    	  window.name = "isReload";  // 在首次进入页面时我们可以给window.name设置一个固定值 
    	}else if(window.name == "isReload"){
    	   console.log("页面被刷新");
    	}

2 使用sessionStorage或cookie来判断

与window.name实现方法类似在首次加载时设置一个固定值 之后判断即可
这里以sessionStorage来为例

 if(sessionStorage.getItem("isReload")){
        console.log("页面被刷新");
   }else{
     console.log("首次被加载");
     sessionStorage.setItem("isReload", true)
   }

 

posted @ 2022-10-04 00:14  IT情深  阅读(253)  评论(0编辑  收藏  举报