Forum Moderators: open

Message Too Old, No Replies

Ping a server to detect if its working before modifying a link's href

         

ocon

10:49 am on Aug 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I know that I can use JavaScript (if enabled in the browser) to override a link's href. For example, Google does this all the time to track outbound links in their search result listings.

Let's say that I'm on example-sites.com, and I want to link to example-destinations.com, but I'm sending the users to example-tracking-and-filter-site.com to track and filter (block directing the user to adult websites) the links. The example-tracking-and-filter-site.com just records the information (and possibly presents a blocked notice) before redirecting the user to the specified url (unless its blocked).

I could use the following code:

<a href="http://www.example-destinations.com/" onmousedown="javascript:this.href='http://www.example-tracking-and-filter-site.com/track.php?url=http%3A%2F%2Fwww.example-destinations.com%2F';"onkeypress="javascript:this.href='http://www.example-tracking-and-filter-site.com/track.php?url=http%3A%2F%2Fwww.example-destinations.com%2F';">Link</a>

Both users with JavaScript and without would land on example-destinations.com, but users with JavaScript would seamlessly pass through example-tracking-and-filter-site.com first.

That works great and fine, but, let's pretend that example-tracking-and-filter-site.com is down for whatever reason, but the other two sites work just fine. User's with JavaScript would get a server error and would not end on example-destinations.com. That's bad.

Is it possible to modify the code to first "ping" or detect that example-tracking-and-filter-site.com is live and working, before modifying the link's href. If the script doesn't get a response within a specified time then link will not modified by the javascript and the user is sent to example-destinations.com directly.

Is a "ping" or detecting function a bad idea or even possible, or would something else be better suited. I'd really like something that would be quick, and unnoticeable to the user.

Thanks

bhukkel

11:40 am on Aug 29, 2010 (gmt 0)

10+ Year Member



First off all it depends of example-tracking-and-filter-site.com is pingable..

And you get a delay in your page, of say max 50ms for ping timeout and perhaps problems with a large concurrent pageviews.

Does example-tracking-and-filter-site.com likes it when you ping it a lot?

I dont think it is a good solution.

Perhaps you can create a cronjob and ping example-tracking-and-filter-site.com say every 15min and save the results in a text file. Every page reads the text file and creates the urls. Now you have max 15 min the wrong urls but less server load.

ocon

11:48 am on Aug 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



It should be noted that I have some control over example-sites.com and full control of example-tracking-and-filter-site.com.

I'd like to limit the code used on example-sites.com to just inline JavaScript, but I can create some kind of special page, or script on example-tracking-and-filter-site.com that the JavaScript pings or checks.

The site example-tracking-and-filter-site.com would be on a dedicated server.

bhukkel

12:06 pm on Aug 29, 2010 (gmt 0)

10+ Year Member



If i understand You right, you want to ping from the clients pc with javascript? That wouldnt be a good idea because a lot of people are behind a proxy or a company firewall where ping is disabled.

Perhaps you can do a small get or head request to example-tracking-and-filter-site.com with javascript from the clients pc and if its ok create the urls you like.

ocon

1:33 pm on Aug 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Ping is only an example, but anything to detect that the server is working would be fine like a get or head request.