Ping URL without redirecting

I have two urls:

  • A is used to redirect to the landing page
  • B is used to track the target (the ping needs to be redirected for that url) without redirecting it.

The above actions should be done concurrently using any click events.

Is it possible to achieve this with 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 the onclick event here gets redirected to both pages.

+3


source to share


2 answers


When clicked, it "redirects" to both pages because the tag <a>

has an attribute target

like _blank

- the URL in the attribute href

will open in a new window and the current window is displayed, the page whose URL is defined in the handler onclick

.

Typically, when you click a link and visit another page, the new page should be displayed in the current window. In this case, you target="_blank"

should delete.

To send a track request and show a new page at the same time. There are 3 solutions:


Solution 1. On the current page, when the link is clicked, send a track request and after a short time redirect to a new page. This all happens in JavaScript, no target="_blank"

, there is 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 that the time interval (in the above code, 300ms

) depends on the network environment of the web application. If it is too small, the HTTP tracking request will not be sent until the page is redirected. If it is too large, a design effect (such as a spinner) can help. This time interval is used because there is no need to wait for a response for a page tracking request, just make sure the request is sent and everything is fine.




Solution 2: When you click the link, go to a new page immediately. And when the new page is open, send an 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 that this solution is imprecise. If a user clicks a link on the original page but was unable to open page X, or the tracking script on page X did not run, such an event will not be tracked.


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

This solution is better than solution 2, but it can be more complex.

0


source


For track purposes only you can use an iframe at B url. When the user clicks on the link, a new window will open with the URL, but the user remains 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 the width and height of the frame to be visible or not.

0


source







All Articles