Reputation: 71
I'm building a page that uses the Slideshare API to pull a limited set of presentations from a specific Slideshare user and output a series of iframes with those presentations embedded.
For the most part the page is working as intended, however one of the presentations that the API is calling was recently deleted, and therefore when the page loads that iframe the browser returns a 410. Unfortunately there is no object in the XML returned by Slideshare that can be used to determine if that particular presentation has been deleted.
Therefore I would like to check for the 410 error prior to adding that particular iframe to the page. What's making this difficult for me is that the error doesn't appear when making the API call, but rather when the embedded iframe loads, so I don't know where or how to make the appropriate check.
Here's the relevant code:
$.ajax({
url: slideShareURL,
dataType: 'XML',
success: function(data) {
var allSlides = x2js.xml2json(data);
allSlides = allSlides.User.Slideshow;
for (i = 0; i < allSlides.length; i++) {
document.getElementById('slides').innerHTML += allSlides[i].Embed;
}
}
});
Notes:
EDIT: Answer provided by Christophe below was almost perfect, but I had to add a closure to make the second ajax call work within the for loop.
Upvotes: 0
Views: 215
Reputation: 71
The answer provided by Christophe put me on the right track, but it wasn't possible to run the second ajax call properly within the for loop. After some more research I learned about closures, and came up with the following:
$.ajax({
url: slideShareURL,
dataType: 'XML',
success: function(data) {
var allSlides = x2js.xml2json(data);
allSlides = allSlides.User.Slideshow;
for (i = 0; i < allSlides.length; i++) {
(function(i) {
var slideURL = allSlides[i].SlideshowEmbedUrl;
$.ajax({
url: slideURL,
success: function () {
document.getElementById('slides').innerHTML += allSlides[i].Embed;
},
error: function () {
console.log('Failed to load');
}
})
})(i);
}
}
});
Upvotes: 1
Reputation: 428
Hmmm,what about this solution :
$(function() {
$.ajax({
url: "www.domain.com/iframe",
dataType: "jsonp",
timeout: 5000,
success: function () {
$("#iframe").attr("src", "www.domain.com/iframe");
},
error: function (parsedjson) {
if(parsedjson.status == "200") {
$("#iframe").attr("src", "www.domain.com/iframe");
} else {
// Handle error
}
}
});
});
Upvotes: 1