We help IT Professionals succeed at work.
Get Started

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

Last Modified: 2014-05-21
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

Watch Question
Web Ninja at large
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE