admin管理员组文章数量:1387310
I think this is impossible, but I'll ask our munity of wizards, maybe someone has a trick.
A client is wants to trigger an event on my js injection platform when someone has reloaded the page more than twice. He refuses to either set a value on a cookie onbeforeonload or to have something on the server side that can count.
Anyone know of a way of counting page reloads on the client side?
I think this is impossible, but I'll ask our munity of wizards, maybe someone has a trick.
A client is wants to trigger an event on my js injection platform when someone has reloaded the page more than twice. He refuses to either set a value on a cookie onbeforeonload or to have something on the server side that can count.
Anyone know of a way of counting page reloads on the client side?
Share Improve this question asked Jul 24, 2015 at 10:10 Asinus RexAsinus Rex 5549 silver badges29 bronze badges 9-
localStorage
orIP
maybe – Tushar Commented Jul 24, 2015 at 10:11 -
A
cookie
is a sort of local storage thats in the hands of the client and entirely on the client side... If your client is adament on not using cookies for some reason, then you might have to go back and tell them that this is exactly why cookies exist. That, and tracking you everywhere you go. – somethinghere Commented Jul 24, 2015 at 10:12 - localStorage is a possibility, but I doubt he'll like that more than cookies. What's the IP idea? – Asinus Rex Commented Jul 24, 2015 at 10:13
- 1 May be passing a parameter say reloadCount in URL, then access this in javascript. – Utkarsh Commented Jul 24, 2015 at 10:21
- 1 @Utkarsh: If I hit the "Reload" button, you can't change the URL in response to that. – Amadan Commented Jul 24, 2015 at 10:24
1 Answer
Reset to default 6The performance.navigation
can be used to distinguish between navigation, history navigation and page reload.
Since you want to know whether the page was refreshed twice, some form of persistency is required because the browser does not keep track of the number of page reloads. For this purpose, the history API seems most sensible, because its state is tied to the page instance.
For example (demo showing number of reloads: https://robwu.nl/s/reloadCount.html):
// On load:
var state = history.state || {};
var reloadCount = state.reloadCount || 0;
if (performance.navigation.type === 1) { // Reload
state.reloadCount = ++reloadCount;
history.replaceState(state, null, document.URL);
} else if (reloadCount) {
delete state.reloadCount;
reloadCount = 0;
history.replaceState(state, null, document.URL);
}
if (reloadCount >= 2) {
// Now, do whatever you want...
alert('The page was reloaded more than twice!');
}
I've tested that the demo works in the following browsers:
- Safari 8+
- Chrome 14+ (probably earlier as well, I didn't bother testing 13-)
- Opera 15+
- Firefox 7+
- Internet Explorer 9+
If you need to support older browsers (that do not support the performance
API), then you could fall back to updating the history state with a timestamp upon unload, and check whether the timestamp (e.g. Date.now()
) in history.state
is close to the current time upon load.
本文标签: javascriptCounting page reloadsStack Overflow
版权声明:本文标题:javascript - Counting page reloads - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744510277a2609839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论