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 http://provincial.ensemblecreative.com/Investment.cfm.

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='http://www.diversifiedwealthmanagement.com/Corey-R--Stich----VP-of-Risk-Management-and-Insurance-Services.e509859.htm';}">Visit Corey R. Stich - VP of Risk Management and Insurance Services Website &raquo;</a></p>

Open in new window

Web Languages and StandardsScripting LanguagesJavaScript

Avatar of undefined
Last Comment
Big Monty

8/22/2022 - Mon
Big Monty

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck