Solved

query problem

Posted on 2014-02-27
3
287 Views
Last Modified: 2014-02-27
Hi.

This script is a Lazy load image script which grabs an image from a YouTube video:

 for (var b = $(".youtube"), e, a, f, c, g, d, h = 0; h < b.length; h++) c = b[h], e = c.id, a = c.style.width, f = c.style.height, g = $(document.createElement("img")), g.attr({
        "class": "lazy",
        "data-original": "http://i1.ytimg.com/vi/" + e + "/maxresdefault.jpg",
        width: '100%',
        height: '100%'
    })

How does i change the script, so that if the image "http://i1.ytimg.com/vi/" + e + "/maxresdefault.jpg" does not exist, It shall change the url to .
http://i1.ytimg.com/vi/" + e + "/default.jpg

Can somebody help?
0
Comment
Question by:bongii
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 500 total points
ID: 39891628
1) You may use like:
var image = new Image(); 
image.src = "image.jpg";
if (image.width == 0) {
  alert("no image");
}

Open in new window

(ref: http://stackoverflow.com/questions/14755183/checking-if-an-image-exists-before-adding-it-as-a-background-image)

Or

2) something like this:
$('img[id$=imgurl]').load(imgurl, function(response, status, xhr) {
    if (status == "error") 
        $(this).attr('src', 'images/DEFAULT.JPG');
    else
        $(this).attr('src', imgurl);
    });

Open in new window

Ref: http://stackoverflow.com/questions/3381663/check-if-image-exists-with-given-url-using-jquery

Thanks.
0
 

Author Comment

by:bongii
ID: 39891638
Thanks, but how can i integrate it in the scrript

This one dosent work:

for (var b = $(".youtube"), e, a, f, c, g, d, h = 0; h < b.length; h++) c = b[h], e = c.id, a = c.style.width, f = c.style.height, g = $(document.createElement("img")), g.attr({
        var image = new Image();
image.src = "image.jpg";
if (image.width == 0) {
  alert("no image");
}
            
            "class": "lazy",
        "data-original": "http://i1.ytimg.com/vi/" + e + "/maxresdefault.jpg",
        width: '100%',
        height: '100%'
    })
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 39891903
Hi, could not test the script, but i think, it should go in the line as follows:


for (var b = $(".youtube"), e, a, f, c, g, d, h = 0; h < b.length; h++)
c = b[h],
e = c.id,
a = c.style.width,
f = c.style.height,
var oriimg = "http://i1.ytimg.com/vi/" + e + "/maxresdefault.jpg";
var defimg = "http://i1.ytimg.com/vi/" + e + "/default.jpg";
var curimg = oriimg,
var image = new Image(),
image.src = oriimg,
if (image.width == 0) {
  var curimg = defimg
},
g = $(document.createElement("img")),
g.attr({
        "class": "lazy",
        "data-original": curimg,
        width: '100%',
        height: '100%'
    }
)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

774 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question