简体   繁体   中英

New Window not Opening when using target="_blank" with onclick

Summary: 1. Need to open a new window to display a survey from a third party.
2. Cannot use JavaScript to open the window (currently using target="_blank". 3. Must use JavaScript to track clicks (currently using onclick). 4. original window must NOT change its content upon clicking.

I have a JSP that is trying to launch a third party survey in a new window. Originally, the code was launching the new window like this:

<a id="customer_survey_button"  href="#" target="_blank" onclick="recordOutboundLink(this, 'Outbound Links', 'blah.com');openChildWindow('http://www.blah.com/f.asp?etcetera','TAKE SURVEY');return false;"  class="genbut">TAKE SURVEY</a>

It worked in FF, but not Chrome. It caused a security problem with Chrome. Something like an 'unsafe JavaScript attempt to access a frame (new window) which loads one domain, while it's origin is another domain. My solution was to open the window using HTML, like this:

<a id="customer_survey_button"  href="http://www.blah.com/f.asp?etcetera" target="_blank" onclick="recordOutboundLink(this, 'Outbound Links', 'blah.com');return false;"  class="genbut">Take Survey</a>

The problem is, that is not opening in a new window. During debugging, I removed the onclick from the above code, and it does open in a new window. I'm not sure if it is something wrong in the JS code...

<script type="text/javascript">
<!--
var pageTracker = _gat._getTracker("<%=googleTrackerId%>");
pageTracker._initData();
pageTracker._trackPageview();

function recordOutboundLink(link, category, action) {
  try {    
    pageTracker._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}
// -->
</script>

...or if it is just a matter of using target and onclick in the same anchor tag. Any thoughts?

I researched elsewhere, and on Stack Overflow, I found the below results, but none seemed to address this situation. For example(s):

This helped in one way, because I was able to test the removal of the "return false". That had the same result as it did for "user1223427"- it rendered the same page in two different windows. It did not help because I need a non-JavaScript way to open the target URL (due to the security issues mentioned). OnClick for google analytics and target _blank. The link does not work?

This one doesn't help, because I'm not using JQuery and I don't understand the connection between the JQuery solution and my situation select onclick that contains _blank

This only applies to getting tracking code to work, not issues with a new window opening: Google Analytics - Download clicks tracking

I thought this one was going to have a good result, but it's solution is a JS one. Bug in external link tracking when opening the link in a new window in Google Analytics?

The recordOutboundLink function is opening the link.

//This is changing the URL to the href of the link you clicked    
setTimeout('document.location = "' + link.href + '"', 100) 

Your second example is returning false from the onclick event. This will prevent the anchor tag from performing it's normal behavior.

If you comment out the setTimeout line, and remove return false from the onclick, you should see the link behave as you want.

You could also modify the setTimeout function to call window.open instead of document.location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM