3

I've two URLs:

  • A is used for redirecting to the landing page
  • B is used for tracking purpose (a ping should be made to this URL without redirecting)

The actions above should take place at same time using any click events.

Is it possible to achieve this using HTML or javascript.

I found this code:

<a href="http://www.omsaicreche.blogspot.com" onclick="location.href='http://www.omsaivatikanoida.blogspot.com';" target="_blank"> Open Two Links With
One Click</a>

but onclick event here redirects to both the pages.

Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
  • 1
    Possible duplicate of [Is it possible to ping a server from Javascript?](http://stackoverflow.com/questions/4282151/is-it-possible-to-ping-a-server-from-javascript) – slevy1 May 19 '17 at 04:33

2 Answers2

0

After click, it "redirects" to both pages because <a> tag have target attribute as _blank -- the URL in the href attribute will be opened in a new window and the current window will display page whose URL is defined in onclick handler.

Normally, when link is clicked and another page is visited, the new page needs to be displayed in current window. In this case, target="_blank" should be removed.

In order to send track request and show new page at the same time. There are 3 solutions:


Solution 1. In current page, when link is clicked, send track request and after a small time, redirect to the new page. It all happens in JavaScript, no target="_blank", no URL in href.

For example:

<a href="javascript:;" onclick="visitPageX()">link</a>

function visitPageX() {
  // send Ajax request to the track URL
  setTimeout(function() {
    location.href = 'http://{pageXURL}';
  }, 300);
}

Please note the time-interval (in the above code, it is 300ms) depends on the web application's network environment. If it is too small, HTTP request for tracking won't be sent before page is redirected. If it is too large, a design effect (such a a spinner) may help. This time-interval is used because for tracking request, page don't need to wait for its response, just guarantee the request is sent and everything is OK.


Solution 2. When link is clicked, go to the new page immediately. And when the new page is opened, send the HTTP request for tracking.

For example:

<a href="http://{pageXURL}">link</a>

// JavaScript code in page X, running when page X is opened.
// send Ajax request to the track URL

Please note this solution is not accurate. If user clicks the link in the original page but failed to open page X, or the tracking script code in page X failed to run, such event won't be tracked.


Solution 3. When link is clicked, go to the new page immediately. In server side program, when the new page X is accessed, send tracking request (from server).

This solution is better than solution 2, but it may bring more complexity.

shaochuancs
  • 12,430
  • 3
  • 40
  • 51
  • Hey @Shaochuancs, do you know where to insert our tracking url in solution 1? I asked a question about it here: https://stackoverflow.com/questions/46851955/how-to-track-outbound-clicks-using-javascript – DomainsFeatured Oct 20 '17 at 15:09
  • Hi, @DomainsFeatured, Sorry for the delay. I think James has already answered your question :) – shaochuancs Oct 23 '17 at 02:39
0

For trackin purpose only you can use iframe on B url . When user click your link a new window will open A url but user will remain on the current page.

<a href="http://www.omsaicreche.blogspot.com"   onClick='document.getElementById("track").src="http://www.omsaivatikanoida.blogspot.com";'" target="_blank">
  Open Two Links With One Click
</a>
<iframe id='track' frameborder="0" scrolling="no" width="100" height="100">

You can set frame width and height to be visible or not.

rheeantz
  • 862
  • 7
  • 12