[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

query problem

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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