New window doesn't open when using target = "_blank" with onclick

Summary: 1. You need to open a new window to display the survey from a third person.
2. You cannot use JavaScript to open the window (currently using target = "_blank". 3. You must use JavaScript to track clicks (currently using onclick). 4. The original window should not change its contents when clicked.

I have a JSP that is trying to run a third party poll in a new window. Originally the code started a 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. This caused a problem with Chrome. Something like "unsafe JavaScript is trying to access the frame (new window) that loads one domain, and its origin is another domain. My solution was to open the window with HTML, like so:

<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 it doesn't open in a new window. While debugging, I removed the onclick from the above code and it will open in a new window. I'm not sure if this 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 is it just a matter of using target and onclick in the same anchor tag. Any thoughts?

I researched elsewhere and in Stack Overflow I found the following results, but no one seems to have addressed this situation. For example (s):

This did the trick in one way, because I was able to verify the removal of "return false". This had the same result as for "user1223427" - it was displaying the same page in two different windows. It didn't help because I needed a non-JavaScript way to open the target URL (due to the security concerns mentioned above). OnClick for Google Analytics and target _blank. The link does not work?

This does not help because I am not using JQuery and I do not understand the relationship between the JQuery solution and my situation select onclick containing _blank

This only applies to the tracking code working, not opening a new window: Google Analytics - Click Tracking Download

I thought this would have a good result, but this solution is JS. Error tracking inbound links when opening a link in a new window in Google Analytics?

+3


source to share


1 answer


The recordOutboundLink function opens a link.

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

      

The second example returns false from onclick event. This will prevent the anchor tag from behaving normally.



If you comment out the setTimeout line and remove return false from the onclick, you can see that the link behaves the way you want it to.

You can also change the setTimeout function to call window.open instead of document.location

+4


source







All Articles