Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Javascript be used to detect a redirected image SRC (in any popular browser)?

Tags:

javascript

dom

I'm guessing nope, but you never know.

  1. Browser loads <img src="http://example.net/lolcat.png">
  2. example.net redirects lolcat.png to http://static.example.net/bandwidth-exceeded.gif

Can I detect this?

Cross-browser isn't important, just looking for an option that will work in any 1 popular browser.

So far my best option is knowing the size of the placeholder image, getting a "maybe" by looking at the image size onload (in browsers that support onload events in image tags), and making a quick XHR request notifying the backend that we've got a maybe.

like image 919
outcassed Avatar asked Nov 02 '10 22:11

outcassed


People also ask

What are JavaScript redirects?

A JavaScript redirect is a piece of JavaScript code that is used to automatically transfer a visitor from a landing page to a different target page.

What is image SRC in JavaScript?

Definition and Usage The required src attribute specifies the URL of an image. Note: The src property can be changed at any time. However, the new image inherits the height and width attributes of the original image, if not new height and width properties are specified.

Can we redirect a page using JavaScript?

In JavaScript, page redirection is simple. The window object's location object is a property. A web page can be redirected in a number of ways.


2 Answers

I checked with the following code:

var a=new Image(); a.src='http://example.com/image.png';
alert( a.width );

which in this case returns 0 since the image doesn't exist.

On link text I found quite a few properties that might be worth checking out. And if that fails you might want to look into jQuery and AJAX.

Edit:
tested on firefox firebug

var a=new Image(); 
a.src='http://l.yimg.com/g/images/photo_unavailable_m.gif';
console.log(a.width); // the image exists and returns 240

var b=new Image(); 
b.src='http://farm3.static.flickr.com/2560/4098180849_729ef4f6ef_m.jpg';
console.log(b.width); // the image does not exist (re-direct) and returns 0
like image 188
patrick Avatar answered Sep 28 '22 07:09

patrick


the answer to your question Can Javascript be used to detect a redirected image SRC (in any popular browser)? is simple as you said , nope! because http doesnt have any option for identifying the redirection. This is similar to detecting url redirection, which is simply a limitation for javascript.

like image 24
Althaf M Avatar answered Sep 28 '22 06:09

Althaf M