Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript检测页面更改?_Javascript_Jquery - Fatal编程技术网

使用javascript检测页面更改?

使用javascript检测页面更改?,javascript,jquery,Javascript,Jquery,如何使用javascript检测我被重定向到的页面 $(window).on("onbeforeunload",()=>{ alert("Something") }) 这段代码永远不会执行(尽管我重新加载页面或单击其他URL)。我正在本地主机上运行脚本。另外,我想知道我被重定向到的页面的URL 这是我的完整HTML: <!DOCTYPE html> <html> <head> <title>Practice

如何使用javascript检测我被重定向到的页面

$(window).on("onbeforeunload",()=>{
    alert("Something")
})
这段代码永远不会执行(尽管我重新加载页面或单击其他URL)。我正在本地主机上运行脚本。另外,我想知道我被重定向到的页面的URL

这是我的完整HTML:

<!DOCTYPE html>
<html>
    <head>
         <title>Practice</title>
         <script 
     src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
        </script>
    </head>

<body>
    <script>
        $(window).unload(()=>{
            alert("Something");
        })

    </script>
    <a href = "http:\\www.youtube.com">Link</a>


</body>
</html>

实践
$(窗口)。卸载(()=>{
警惕(“某物”);
})
也许您可以
alert()
或者做任何您想做的事情,然后重定向到
url
,如下面的示例所示

$('a')。在('click')上,函数(e){
e、 预防默认值();
让url=this.href;
警报(`您将离开此页面,将被重定向到:${url}`)
window.location.href=url;
})

在现代浏览器(IE8+、FF3.6+、Chrome)中,您只需收听窗口上的
hashchange
事件即可

if ("onhashchange" in window) {
  alert("The browser supports the hashchange event!");
}
function locationHashChanged() {
  if (location.hash === "#somecoolfeature") {
    somecoolfeature();
  }
}
window.onhashchange = locationHashChanged;

事件名称只是
beforeunload
而不是
onbeforeunload
。但是在此期间,您不允许调用
alert()
,它应该只返回一个字符串。有没有办法检测我被重定向到的URL?没有。