0

Within an object constructor there's a method named addToViewport(), which has the role of simply displaying an image after preloading it:

window.onload = function(){

            function ViewportObject(){
                this.src = "https://chart.googleapis.com/chart?cht=qr&chs=500x500&chl=asdasdasd&choe=UTF-8&chld=L|0";
                this.addToViewport = function(){
                    // Determine the DOM object type to create
                        var DOM_elem = "img",
                            obj = $(document.createElement(DOM_elem)),
                            img = new Object();

                            obj.addClass("object");
                            obj.css({"z-index":"100","width":"300px","height":"auto"});

                            // Preload the image before rendering it and computing its size
                            img.src = this.src;
                            img.onload = function(){

                                obj.attr("src",this.src);
                                obj.appendTo("body");
                            }                       

                }
            }

            var o = new ViewportObject();   
            o.addToViewport();
        }

The problem I've come across is that the script doesn't enter the "onload" event handler block, so the image doesn't get displayed. I put together a web page with the same script as above on http://picselbocs.com/test/ for you to check out live.

What is it that I'm doing wrong here?

3
  • Images loaded from the browser cache don't fire the .load event, which is why the jQuery docs say you shouldn't use it for images. Plugins exist to rectify this problem; see this question Commented Sep 24, 2012 at 18:37
  • I've tried the same thing with a different image that I had never loaded before, and the behavior is the same. This doesn't seem to have anything to do with browser caching. Or am I missing something? Commented Sep 24, 2012 at 18:39
  • Add the img to the DOM, with display: none;, use .load() from jQuery on the image object (selected via jQuery), then show once it's loaded? Commented Sep 24, 2012 at 18:40

1 Answer 1

3

Try this:

change this

img = new Object();
....
img.src = this.src;
img.onload = function(){

    obj.attr("src",this.src);
    obj.appendTo("body");
} 

to

img = new Image();
....
img.onload = function(){

    obj.attr("src",this.src);
    obj.appendTo("body");
} 
img.src = this.src;
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, just switching img = new Object() to img = new Image() fixed it. I didn't realize I was creating an Object and not an Image. Silly me... Many thanks!
If this helped you, please hit the green checkmark next to my answer!
so.com doesn't let me choose an answer within the first 10 minutes after submitting the question, so I couldn't do it sooner. Thanks again!
you really do want to reverse the .onload and .src commands though, it will prevent caching errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.