Reputation: 11
I have a requirement to fire up a confirm box method upon a click of any anchor tag based link in my application if that redirects the user to a different domain.
So lets suppose I am using https://test.com and I click on a anchor tag based link
<a onclick='window.open('https://www.google.co.in','_blank')>Click Me</a>
Then I need to fire up a custom confirmation popup method which if user confirms it redirects the user to the mentioned link.
Edit: I would like to keep my anchor tag the way it is above. Is there some way I can capture this event and then fire up my method.
Upvotes: 0
Views: 164
Reputation: 61984
You could do with having a function which runs on the "onclick" event, which then contains the code which pops up the "confirm" box.
Demo:
function openWindowWithConfirm(url) {
if (confirm("Are you sure you want to go to " + url + "?")) {
window.open(url, '_blank');
}
}
<a onclick='openWindowWithConfirm("https://www.google.co.in")'>Click Me</a>
P.S. Terminology note: opening a new window is not the same as "redirection". A redirect is actually normally a term for a header sent out by the server in response to a request, telling the browser to go somewhere else instead. But people sometimes use it to mean doing a client-side navigation of the current window to a new URL. Certainly though, opening a new window doesn't redirect anything - the existing window stays on the same page.
Upvotes: 2