Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP jQuery Cookbook

You're reading from   PHP jQuery Cookbook jQuery and PHP are the dynamic duo that will allow you to build powerful web applications. This Cookbook is the easy way in with over 60 recipes covering everything from the basics to creating plugins and integrating databases.

Arrow left icon
Product type Paperback
Published in Dec 2010
Publisher Packt
ISBN-13 9781849512749
Length 332 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Vijay Joshi Vijay Joshi
Author Profile Icon Vijay Joshi
Vijay Joshi
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

PHP jQuery Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Handling Events with jQuery FREE CHAPTER 2. Combining PHP and jQuery 3. Working with XML Documents 4. Working with JSON 5. Working with Forms 6. Adding Visual Effects to Forms 7. Creating Cool Navigation Menus 8. Data Binding with PHP and jQuery 9. Enhancing your Site with PHP and jQuery Firebug Index

Checking for missing images


If you are displaying some images in the browser and unfortunately some of the images are missing, the browser will either display a blank space or will display a placeholder with a cross symbol. This surely looks ugly and you would definitely want to avoid it. Wouldn't it be good if you had a method with which you could find missing images or those that failed to load?

After going through this recipe you will be able to detect missing images and replace them with an image of your choice.

Getting ready

Get three or four images from your computer. You will need these with this recipe. Also keep the jQuery file handy. Create another image using a program like paint with text "Could not load image" written on it. This will be the default placeholder for images that fail to load.

How to do it...

  1. Create a new file in the chapter1 directory and name it as error.html.

  2. Place a DIV in the page, which will be filled with images. Also, write some CSS to style the DIV and the images.

    <html>
      <head>
        <title>Check missing images</title>
        <style type="text/css">
        div
        {
          border:1px solid black;
          float:left;
        }
        img
        {
          width:180px;
          height:200px;
          margin:10px;
        }
        </style>
        
      </head>
      <body>
        <div id="imageContainer"></div>
      </body>
    </html> 
  3. Write the jQuery code that creates an array of image names. Intentionally put some random names of images that do not exist. Then fill the DIV by creating image tags from this array. Next, bind the error() event handler to the image elements.

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function ()
      {
        var images= ['himalaya.png', 'chaukori.png', 'tree.png', 'noSuchimage.png',  'anotheNonExistentImage.png'];
        var html = '';
        $.each(images,function(key, value)
        {
          html+= '<img src="'+value+'" />';
        });
        $('#imageContainer').html(html);
    
        $('img').error(function()
        {
          $(this).replaceWith('<img src="missing.png" alt="Could not load image">');
        });
      });
    </script>
  4. Run the error.html file in a browser. You will see that the last two images, which do not exist, have been replaced by another image that says Could not load image.

How it works...

First we use jQuery's $.each() method to iterate in the array that holds image names and fills the DIV by creating image tags.

Then there is an error() event handler attached to image tags. This gets executed when the image fails to load or has a broken src attribute. The event handler for the error() method replaces the nonexistent image with another image of our choice. In our case we replace it with an image that we have created and that says Could not load image.

See also

  • Binding and unbinding elements, which explains the basics of adding events.

You have been reading a chapter from
PHP jQuery Cookbook
Published in: Dec 2010
Publisher: Packt
ISBN-13: 9781849512749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image