#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    who wants to know?
    Posts
    32
    Rep Power
    11

    Checking to see if an image URL exists (JavaScript)


    Wondering if this is possible. I've seen a couple of topics on this forum relating to a similar process checking for website URLs, I just thought this might be different enough to warrant a new approach.

    Here's my code:

    Code:
    <script>
    
    function validate()
    {
    x=document.myForm
    mov=x.myInput.value
    scene=x.myOptions.value
    
     if (scene=="blank")
      {
       document.forms[0].reset();
       document.forms[0].elements[0].blur();
       return;
      }
    
     if (mov=="blank")
      {
       document.forms[0].reset();
       document.forms[0].elements[0].blur();
       return;
      }
    
     document.getElementById('imag').src=x.myOptions.value + x.myInput.value
    
    }
    
    </script>
    
    
    <form name="myForm">
    
    Search for Images :: 
    	<select name="myInput" size="1">
    
                <option selected value="blank">-- Movie Index --</option>
    	    <option value="_Mov_1.JPG">First Movie
    	    <option value="_Mov_2.JPG">Second Movie
    	    <option value="_Mov_3.JPG">Third Movie
    	    <option value="_Mov_4.JPG">Fourth Movie
    
    	</select> 
    
    	<select name="myOptions" size="1" onchange="validate()">
    
                <option selected value="blank">-- Scene Index --</option>
    	    <option value="Scene_One">Scene One
    	    <option value="Scene_Two">Scene Two
    	    <option value="Scene_Three">Scene Three
    	    <option value="Scene_Four">Scene Four
    	    <option value="Scene_Five">Scene Five
    	    <option value="Scene_Six">Scene Six
    	    <option value="Scene_Seven">Scene Seven
    
    	</select> 
    </form>
    
    <img id="imag" src="splash.JPG">

    What I want to do is check and see if there is actually an image called, for example, "Scene_One_Mov_1", and if there isn't redirect the user to an error message. Would probably be written in an "if ... else" statement.

    Thank you for your time

    Meatwad
  2. #2
  3. (not) Banned
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2004
    Location
    Toronto, CANADA
    Posts
    598
    Rep Power
    11
    Hmm... sorry, not about to go through all that mess. Here's one way to get it done:

    Code:
    <img src='invalid_url.png' onerror="alert('image does not exist/you are not connected to the internet');"
    If you were to use a form to gather the urls to check, and then use a function to change the url of a 1x1 pixel image. If there was a problem, the onerror event would be triggered, and you can execute whatever code you want.
    Last edited by obi_wonton; August 27th, 2004 at 06:54 PM.
    May the source be with you - obi_wonton
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    who wants to know?
    Posts
    32
    Rep Power
    11

    shweet.


    Originally Posted by obi_wonton
    Hmm... sorry, not about to go through all that mess. Here's one way to get it done:

    Code:
    <img src='invalid_url.png' onerror="alert('image does not exist/you are not connected to the internet');"
    If you were to use a form to gather the urls to check, and then use a function to change the url of a 1x1 pixel image. If there was a problem, the onerror event would be triggered, and you can execute whatever code you want.

    Perfect fit! Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo