Ahm*_*med 158 javascript page-refresh
我想检查有人试图刷新页面的时间.
例如,当我打开页面时没有任何反应,但是当我刷新页面时会返回警报.
Rah*_*ain 203
知道页面实际重新加载的更好方法是使用大多数现代浏览器支持的导航器对象.
//check for Navigation Timing API support
if (window.performance) {
console.info("window.performance works fine on this browser");
}
if (performance.navigation.type == 1) {
console.info( "This page is reloaded" );
} else {
console.info( "This page is not reloaded");
}
Run Code Online (Sandbox Code Playgroud)
来源:https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API
Dit*_*Sky 39
第一步是检查sessionStorage
一些预定义的值,如果它存在警告用户:
if (sessionStorage.getItem("is_reloaded")) alert('Reloaded!');
Run Code Online (Sandbox Code Playgroud)
第二步是设置sessionStorage
一些值(例如true
):
sessionStorage.setItem("is_reloaded", true);
Run Code Online (Sandbox Code Playgroud)
会话值一直保持到页面关闭,因此仅当页面重新加载到包含该站点的新选项卡中时才会起作用.您也可以以相同的方式保持重新加载计数.
Иль*_*ько 23
window.performance.navigation
属性在Navigation Timing Level 2规范中已弃用.请改用界面.PerformanceNavigationTiming
这是一项实验技术.
在生产中使用它之前,请仔细检查浏览器兼容性表.
类型只读属性返回表示导航类型的字符串.值必须是以下之一:
导航 - 通过单击链接,在浏览器的地址栏中输入URL,表单提交或通过除重新加载和back_forward之外的脚本操作进行初始化来启动导航,如下所示.
重新加载 - 导航是通过浏览器的重新加载操作或location.reload()
.
back_forward - 导航是通过浏览器的历史遍历操作.
预渲染 - 导航由预渲染提示启动.
此属性为只读.
function print_nav_timing_data() {
// Use getEntriesByType() to just get the "navigation" events
var perfEntries = performance.getEntriesByType("navigation");
for (var i=0; i < perfEntries.length; i++) {
console.log("= Navigation entry[" + i + "]");
var p = perfEntries[i];
// dom Properties
console.log("DOM content loaded = " + (p.domContentLoadedEventEnd - p.domContentLoadedEventStart));
console.log("DOM complete = " + p.domComplete);
console.log("DOM interactive = " + p.interactive);
// document load and unload time
console.log("document load = " + (p.loadEventEnd - p.loadEventStart));
console.log("document unload = " + (p.unloadEventEnd - p.unloadEventStart));
// other properties
console.log("type = " + p.type);
console.log("redirectCount = " + p.redirectCount);
}
}
Run Code Online (Sandbox Code Playgroud)
tec*_*bar 14
第一次有人访问该页面时存储cookie.在刷新时检查您的cookie是否存在以及是否存在,请提醒.
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!');
}
}
Run Code Online (Sandbox Code Playgroud)
并在你的身体标签:
<body onload="checkFirstVisit()">
Run Code Online (Sandbox Code Playgroud)
Ali*_*our 14
我编写了这个函数来同时使用旧的window.performance.navigation
和新的方法来检查这两种方法performance.getEntriesByType("navigation")
:
function navigationType(){
var result;
var p;
if (window.performance.navigation) {
result=window.performance.navigation;
if (result==255){result=4} // 4 is my invention!
}
if (window.performance.getEntriesByType("navigation")){
p=window.performance.getEntriesByType("navigation")[0].type;
if (p=='navigate'){result=0}
if (p=='reload'){result=1}
if (p=='back_forward'){result=2}
if (p=='prerender'){result=3} //3 is my invention!
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
结果说明:
0:点击链接,在浏览器地址栏中输入网址,提交表单,点击书签,通过脚本操作初始化。
1:单击重新加载按钮或使用Location.reload()
2:使用浏览器历史记录(Bakc 和 Forward)。
3:预渲染活动如<link rel="prerender" href="//example.com/next-page.html">
4:任何其他方法。
我在这里找到了一些信息Javascript Detecting Page Refresh.他的第一个建议是使用隐藏字段,这些字段往往通过页面刷新来存储.
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
}
}
Run Code Online (Sandbox Code Playgroud)
<html>
<body onLoad="JavaScript:checkRefresh();">
<form name="refreshForm">
<input type="hidden" name="visited" value="" />
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
如果
event.currentTarget.performance.navigation.type
Run Code Online (Sandbox Code Playgroud)
回报
0 =>用户只需键入Url
1 =>页面重新加载
2 =>单击后退按钮.
归档时间: |
|
查看次数: |
251290 次 |
最近记录: |