我希望用户在点击浏览器刷新按钮时被重定向到不同的页面,但我无法这样做。我已经尝试过使用onbeforeunload但它似乎不适用于我的要求,因为它给出了Leave和Stay的消息,但即使这样似乎也不适用于chrome。那么如何在点击浏览器刷新按钮时将用户重定向到其他页面。
我试过的代码:
<html>
<body>
<script>
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
</script>
</body>
</html>
此致,Rushabh
你必须在页面加载时检查performance.getEntriesByType
。这是你如何尝试。
function checkEvt(){
var evTypep=window.performance.getEntriesByType("navigation")[0].type;
if (evTypep=='reload'){
window.location.replace("http://www.stackoverflow.com");
}
}
checkEvt();
你可以试试这个:
if (performance.navigation.type == 1) {
location.href='https://www.google.com';
} else {
console.log( "Not reloaded");
}