Link to home
Start Free TrialLog in
Avatar of lonnyo

asked on

Warn user they are leaving a site and open link in a new window

I have a website that requires the user must be warned anytime they are leaving the site.  After presenting the warning with a popup, the user needs to have the option to click "OK" or "Cancel".  The link needs to open in a new window.

I have this working except when the user clicks cancel, it keeps the open window and is blank. I need to get it set so the message occurs first before opening the new tab/window or the new tab/window closes when the user clicks cancel.

I also have the javascript code in the href and my research tells me that is not the best way to do this, but I am not sure how to set it up any other way.

If you want to see this in action, the preview link is

Here is the code I have so:
<p><a target="_blank" href="javascript:if (window.confirm('You are now leaving the Provincial Bank website. The link is provided for your convenience. Any products and services accessed through this link are not provided, endorsed or guaranteed by Provincial Bank.  Provincial Bank is not liable for content, links, privacy policy, or security of this website.')) {document.location='';}">Visit Corey R. Stich - VP of Risk Management and Insurance Services Website &raquo;</a></p>

Open in new window

Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial