Solved

query problem

Posted on 2014-02-27
3
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

691 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