Still learning and need some help please.
This code example checks the validity of a url:
function check_URL() {
var url = "http://" + localStorage['t'] + ".somewhere.com";
$.getJSON("http://query.yahooapis.com/v1/public/yql?"+
"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+
"%22&format=xml'&callback=?",
function(data){
if(data.results[0]){
console.log("yes");
}
else {
console.log("no");
alert(url + " is not a valid URL or is down.");
}
}
);
};
It does exactly what I want (and I found it here!). But, I need to check additional urls too.
How can I do that with an .each? I’m just too new to this stuff and an example will help my learning.
Also, if it helps, the domain “somewhere” used in the example will always be the same.
UPDATE: Thanks to Thiago who helped point me in the right direction.
function check_URL() {
var url = "http://" + localStorage['t'] + ".somewhere.com";
var url1 = "http://" + localStorage['t1'] + ".somewhere.com";
var url2 = "http://" + localStorage['t2'] + ".somewhere.com";
var url3 = "http://" + localStorage['t3'] + ".somewhere.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(url + " is not a valid URL or is down.");
}
}
);
});
};
This seems to find any of the urls that don’t validate but the alert is broken.
Ideas?
Thanks!
FINAL UPDATE: Working solution posted below.
Here’s the solution I used. Thanks to @BaylorRae’ for the guidance!