Reputation: 342
I want to achieve the following:
user clicks on the inbound website link and javascript/jquery executes something first, and when execution is over then loads next page.
I tried several techniques I found around but nothing really works for me (or I did something wrong).
Specifically, I want to run JS animation before moving to next page.
Upvotes: 0
Views: 102
Reputation: 28414
Here are the steps to do this:
click
listener to the linke.preventDefault()
to prevent direct redirectionawait
to continue when it finisheshref
attribute of the clicked linkredirectLink.addEventListener("click", redirect);
async function redirect(e){
e.preventDefault();
await runAnimation();
console.log("redirecting...");
window.location = e.target.href;
}
function runAnimation(){
return new Promise((resolve, reject) => {
console.log("running animation...");
resolve();
});
}
<a href="https://www.google.com/" id="redirectLink">Redirect</a>
Upvotes: 2