How to detect what page i am being redirected to using javascript.
$(window).on("onbeforeunload",()=>{
alert("Something")
})
This code never executes (despite me reloading the page or clicking on other URLs). I am running my scripts on localhost. Also, i would like to know the URL of the page that i am being redirected to.
This is my full 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>
In modern browsers(IE8+, FF3. 6+, Chrome), you can just listen to the hashchange event on window.
Answer: Use the window. location. href Property location. href property to get the entire URL of the current page which includes host name, query string, fragment identifier, etc.
Maybe you can alert()
or do whatever you want and then redirect to the url
like the following example?
$('a').on('click', function(e){
e.preventDefault();
let url = this.href;
alert(`You're leaving this page, would be redirected to : ${url}`)
window.location.href = url;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href = "https://stackoverflow.com/help/mcve">Link1</a>
<a href = "https://stackoverflow.com/help/deleted-questions">Link2</a>
In modern browsers(IE8+, FF3.6+, Chrome), you can just listen to the hashchange
event on window.
if ("onhashchange" in window) {
alert("The browser supports the hashchange event!");
}
function locationHashChanged() {
if (location.hash === "#somecoolfeature") {
somecoolfeature();
}
}
window.onhashchange = locationHashChanged;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With