I have the following code to check if a YouTube ID is valid. Its a bit of a hack, but works well. It checks the size of the returned thumbnail image. If its not a valid YouTube video, you get the standard placeholder thumbnail thats only 120px. The regular valid thumbnail is 160px so you can test if its a legitimate ID.
The problem I have, is that this if statement doesn't wait for the image to load and test for width, so doesn't catch the change of ok_to_add variable to true (or 1). How can I make the script wait for the img.onload function result and confirm before proceeding? I will need several of these checks all with different criteria for different players.
var ok_to_add = 0;
if (this_player_item == 'youtube') {
var img = new Image();
img.src = "http://img.youtube.com/vi/" + this_player_id + "/mqdefault.jpg";
img.onload = function () {
var img_width = img.width;
if(img_width < 130) {
var thisconfirm = confirm("This YouTube ID doesn't appear to be valid. Proceed anyway?");
if (thisconfirm) {
ok_to_add = 1;
} else {
return false;
}
} else {
ok_to_add = 1;
}
}
alert(ok_to_add);
}