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