【问题标题】:getting URL before unloading in IE在 IE 中卸载之前获取 URL
【发布时间】:2010-09-21 09:55:23
【问题描述】:

我可以在页面卸载之前获取更新后的 URL。

我想检查这个 URL 是否仍然是用户在刷新期间使用的 URL。我想在用户切换到另一个页面之前做一些事情。

用例:

[正常] 浏览器地址栏:www.mypage.com/page1.html 用户点击重新加载 没有弹出通知显示 页面重新加载

[异常] 浏览器地址栏:www.mypage.com/page1.html 用户修改地址栏:www.anotherpage.com 在加载 www.anotherpage.com 之前显示 POP UP 通知

我想知道如何在页面卸载前获取地址栏的当前值 我试过这个代码sn-p:

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??!");
    }
}

请帮帮我。

【问题讨论】:

    标签: internet-explorer url onbeforeunload


    【解决方案1】:

    document.location.href 总是给出运行脚本的页面的 URL。它没有改变。

    您无法获取用户导航到的页面的 URL。您可以在所有传出链接上放置 click 处理程序以捕获这些情况,但出于明显的隐私原因,您当然无法读取用户在地址栏中输入的地址。

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多