开发者

On click, redirecting to another URL if server slow?

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 开发者_StackOverflow社区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...


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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜