?
Solved

query problem

Posted on 2014-02-27
3
Medium Priority
?
296 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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
Suggested Courses

752 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