Make A Ping To A Url Without Redirecting
Solution 1:
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>
functionvisitPageX() {
// send Ajax request to the track URLsetTimeout(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.
Solution 2:
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.
<ahref="http://www.omsaicreche.blogspot.com"onClick='document.getElementById("track").src="http://www.omsaivatikanoida.blogspot.com";'" target="_blank">
Open Two Links With One Click
</a><iframeid='track'frameborder="0"scrolling="no"width="100"height="100">
You can set frame width and height to be visible or not.
Post a Comment for "Make A Ping To A Url Without Redirecting"