This example validates urls correctly. How can I identify which one(s) didn’t validate? I want to trigger a single alert in the else section or get the value(s) to perhaps use elsewhere.
function check_URL() {
var url = "http://" + localStorage['t'] + ".tumblr.com";
var url1 = "http://" + localStorage['t1'] + ".tumblr.com";
var url2 = "http://" + localStorage['t2'] + ".tumblr.com";
var url3 = "http://" + localStorage['t3'] + ".tumblr.com";
var urlArray = ['url', 'url1', 'url2','url3'];
$(urlArray).each(function (urlItem) {
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(urlItem)+
"%22&format=xml'&callback=?",
function(data){
if(data.results[0]){
console.log("yes");
}
else {
console.log("no");
alert("? is not a valid URL or is down.");
}
}
);
});
};
Thanks from the newbie for your help!
To loop through an array you need to use jQuery.each().
Also, you don’t need to wrap the variable names in a string. Doing so will just return
"url"rather than the value of the variable.