Detecting users from China

Hi guys,

Because China has blocked YouTube, we need to find another way to display our videos to our users in China. Ive done a bit of Googling, and the best Ive seen is this:

var image = new Image();
image.onload = function(){
	// The user can access youtube
	document.write('<p>display youtube</p>');
}
image.onerror = function(){
	// The user can't access youtube
	document.write('<p>display youku movie</p>');
}
image.src = "http://youtube.com/favicon.ico";

So - as you can see - the browser is attempting to download the http://youtube.com/favicon.ico image. If it cant download it (because YouTube has been blocked) then the user should see a YouKu movie (Chinese equivalent of YouTube); and if the user can download the favicon, then display YouTube movie.

However, this doesnt seem to work! Im using the Hola addon for Chrome to test this - so I dont knwo if its a problem with the addon or a problem with the code. What do you think?

Many thanks

There may be a timing incompatibility between the onload/onerror test and the document.write as once the onload/onerror trigger the page will have already finished loading and so document.write will overwrite the entire page.

Ah I see. So I need something that continually checks for 5 or so seconds (which is very reasonable for a little icon to load) to see if that image has downloaded, and if it hasnt downloaded within 5 seconds, then display the youko movie?

No - the onload and onerror testing is fine - the change you need to make is to get rid of the antiquated document.write calls and replace them with more modern JavaScript that updates the content of an already loaded web page.