我可以在页面卸载之前获取更新的 URL。
我想检查这个 URL 是否仍然是用户在刷新期间使用的 URL。我想在用户更改到另一个页面之前做一些事情。
用例:
【正常】浏览器地址栏:www.mypage.com/page1.html 用户点击重新加载 无弹窗提示显示 页面重新加载
【异常】浏览器地址栏:www.mypage.com/page1.html 用户修改地址栏:www.anotherpage.com 加载www.anotherpage.com前弹出通知
我想知道如何在页面卸载之前获取地址栏的当前值我尝试了这个代码片段:
var currPage;
window.onload = function(){
currPage = document.location.href;
}
window.onbeforeunload = function (e){
var nextLocation = document.location.href; // I assumed this value will update always. but NOT! :((
if( currPage != nextLocation )
{
alert("You're leaving now??!");
}
}
请帮我。