/    Sign up×
Community /Pin to ProfileBookmark

Modifying a page on an image load failure

I’m an experienced server side application developer, but don’t have much experience on the client side, so bear with me.

Here’s what I’d like to do: Enter some URL into a browser and pull down an HTML page from the server. Inside the HTML there’s a link to an image on another server. Now my problem is that this image may exist and be fetchable, or it’s possible that the server is not running, and thus that image cannot be fetched.

So what I need to do is this: if the image is fetched, everything is great and just continue processing. If it cannot be fetched, then all of the text on the page should change from what it normally says to information to the user to exlain what happened and what he should do.

My question is how to do this. I believe that this can be done with JavaScript, and if I’m given a few pointers to things to look at I can probably fill the details in myself.

Thanks for any help.

Sander

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mrhooJun 03.2008 — If an image is not found it returns an error- you can add an error handler to the image element.

When an image loads, a property of the image element named 'complete' is set to true. You can set its onload handler or use a polling timer to test for the 'complete' property.

But the server is best for finding out if a file exists at a remote location.
Copy linkTweet thisAlerts:
@smithsaauthorJun 03.2008 — If an image is not found it returns an error- you can add an error handler to the image element.

When an image loads, a property of the image element named 'complete' is set to true. You can set its onload handler or use a polling timer to test for the 'complete' property.
[/QUOTE]


Okay, I think I understand at least conceptually. I try to load the image and the onload handler can decide whether it succeeded or not. Then, it can call some other function to do whatever is necessary. Is that the right way to look at it?


But the server is best for finding out if a file exists at a remote location.[/QUOTE]


Agreed, but it's a bit more complicated in my case. The HTML file and the image file are going to be served by 2 different servers that cannot see each other. Hence, I can't just have them communicate which is what I'd like to do. I have to do all of this fancy footwork because the browser is the only thing that has visibility to both places, hence I need some JavaScript.
×

Success!

Help @smithsa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...