Solved

query problem

Posted on 2014-02-27
3
285 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now