How to check whether an image is present or not in a webpage using JavaScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • parupudi
    New Member
    • Jul 2010
    • 1

    #1

    How to check whether an image is present or not in a webpage using JavaScript

    can somebody help me in writing a code that checks a website for the presence of an image.

    For example, If an image A.jpeg is used in a web page, then the java script file should check for the presence of the image. If not, it must return error message.

    I wrote the code like this:

    Code:
    <SCRIPT language="JavaScript">
    
    	if (A.gif)
    	{
    
    		window.location="http://www.google.com";
    	}
    
    }
    else
    	alert("There is no match");
    
    </SCRIPT>

    Can some experts guide me in solving this?

    Thanks in advance..

    Waiting for replies...
    Last edited by Dormilich; Jul 2 '10, 11:46 AM. Reason: Please use [code] tags when posting code
  • ashsa
    New Member
    • Feb 2007
    • 45

    #2
    Due to cross site scripting restrictions, I believe that this is achievable only if the page is in the same domain as that of your javascript file.

    You can use the ajax object XmlHttpRequest to open a connection to the target url and get the response text to see if such an image is there in the source HTML. However, I am afraid this will not check if the image is physically present on the server as it gets only the html source of the page.

    Hope that helps.

    Comment

    Working...