0

Is it possible (in Javascript, ajax, other e.g. on the client site) to redirect the user to another URL if first URL slow to answer (when he clicks on a link) ?

A href=URL1 but if no answer from server1 after 1 second, redirection to URL2 (another server)

I was thinking about something like on event onclick : redirection to URL1, timer, redirection to URL2 but if server 1 is not responding, the code after won't be executed...

Or then using AJAX, but I don't see how

The case ; a click on a page (a href=urltracking), urltracking redirect to URL2, but urltracking server can be slow...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Matthieu
  • 361
  • 2
  • 6
  • 16

5 Answers5

0

You can use setTimeout Function of javascript.

setTimeout("function()",1000);

here in function you need to write code for redirection.

Also see this question for complete reference.

Correct me if I am wrong.

Community
  • 1
  • 1
  • Thanks for the answer. I thought about this function but there is 2 problems ; it seems that internet explorer doesnt't have the same definition for setTimeOut, and it won't work even in Firefox. first line : gotoURL1, second line setTimeOut --> second line won't be executed (cause the brower will wait for the first line to execute) – Matthieu Feb 19 '11 at 10:06
  • I think you meant to use `setTimeout` instead of `setTimeOut`. – jhartz Feb 20 '11 at 04:37
  • Nice suggestion. However, you have editing privileges as well ! –  Feb 22 '11 at 05:17
0

Are both these sites your own? Maybe you should buy a load balancer. This is essentially a server that monitors performance of two webservers and redirects requests to the one that is least busy.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • None of the sites are my own. One is for tracking clicks (tracking company) ; the other is a client --> he doesn't want to block his user if the tracking company experiment slowness – Matthieu Feb 19 '11 at 10:12
  • In that case, it is best to use AJAX. You can send the both requests simultaniously. – GolezTrol Feb 19 '11 at 10:57
  • thx, how would you do that? the tracking URL will be taken into consideration? – Matthieu Feb 19 '11 at 11:07
  • Usually a tracking url is just a url with a specific code that doesn't return any useful data. The request is just done for logging and the code in the url is for identification. You can use jQuery.Ajax or the jQuery.Get shorthand function to retreive that url. – GolezTrol Feb 20 '11 at 01:25
  • Another, more low level approach is to create a 1x1 hidden image that refers to the url. The image won't be valid, but that's no problem since it is hidden. At least the request is fired when the page is loaded. Usually a browser will open at least 4 connections for downloading images simultanously, so your 'tracking pixel' can be loaded while you fire the other request too. Advantage of Ajax over pixel is that Ajax can be fired at any time after the page is loaded, while the pixel is loaded directly after DOM ready. – GolezTrol Feb 20 '11 at 01:26
  • Yes, but i don't want to fire the tracking pixel when the page is loaded but when the user click ; that is the problem – Matthieu Feb 20 '11 at 04:28
  • All right, skip the pixel solution then. :) You can still use Ajax as I mentioned. You don't even have to wait for the response. The request is sent anyway, even if it takes to long for the response to return. – GolezTrol Feb 20 '11 at 10:31
  • Ok! How would you that in Ajax? I was planning this : on the onclick event : shoot the tracking URL in the XMLHttpRequest and just after change the window URL. Looks good but the tracking URL is never taken into consideration – Matthieu Feb 20 '11 at 17:00
0

I'm afraid it's not possible in this way. You can measure the response time by "timeout check" with a dummy AJAX call, if the target URL lays in your domain. Something like "send test GET, if server doesn't respond in XX secs then rewrite URLs to backup sites". But it's not suitable for general use.

Martin Maly
  • 237
  • 2
  • 6
0

I always try to avoid situations where a 3rd party site can slow down my own site.

Perhaps you can make the call in some asynchronous form instead? Have a piece of javascript fire within the DOM ready event that makes the call to the tracking server instead, something like (in jQuery):

$(function(){
    var tracker = new Image();
    tracker.src = "http://tracker.com/path/to/tracker
});

other methods that can work are just a plain old tag, or an , etc. The key being that this loads after your page, and not before it. The tracking server will never know the difference.

Gal
  • 5,537
  • 1
  • 22
  • 20
  • Thanks for this, definitely would do that for tracking page views. Here I don't know if it's possible (to use asynchronous call) ; the tracker is on a click (ie href="http://tracker.com/path/to/tracker" redirecting to final URL) – Matthieu Feb 19 '11 at 10:29
  • Thats what I assumed, it should still work technically. If you just link directly to the destination URL, then fire the tracker off as I described, it should have the same net effect. I guess this can't work if you have no control over how they link to you. – Gal Feb 19 '11 at 11:44
  • One caveat of course is that it will cause a 2nd page view to your page, since the IMG or IFRAME will follow the redirect too. But may be worth it if this is your only option and your priority is performance. – Gal Feb 19 '11 at 11:45
  • I'm not sure to understand ; your option is to trigger the tracker on the destination page? The problem is that the tracker has to count the clicks from page 1 to page 2 (I can go to page 2 even if I was not in page 1). Or if I fire the tracker code just after the click, it won't work ; as the page change, the tracker won't have time to be triggered – Matthieu Feb 19 '11 at 17:28
0

Pay more money for a better server(s) and in extreme case with a load balancer. You should never need to do something like this client side.

jennas
  • 2,444
  • 2
  • 25
  • 31
  • Sure, but is it possible on client side ? – Matthieu Feb 19 '11 at 11:05
  • If you must, I would start a timeout function that will redirect with an inline script first thing in the body tag. Then I would clear the time out when the DOM is ready. But this will also redirect people with a slow connection. Again do it server side if you can. – jennas Feb 19 '11 at 22:37
  • My question was not properly writen (I edited it). I want to do that on a click ; user click on an urltracking, I don't want to block him if urltracking server is slow... – Matthieu Feb 20 '11 at 04:35