jevgen / explorercanvas

Automatically exported from code.google.com/p/explorercanvas
Apache License 2.0
0 stars 0 forks source link

onreadystatechange event fails when lazy-loading #79

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Lazy load excanvas.js
2. Dynamically generate some canvas elements

What is the expected output? What do you see instead?
You should see excanvas convert your canvas code to VML. Instead, you see
nothing (the conversion does not take place).

What version of the product are you using? On what operating system?
This is not specific to any particular version of IE or Windows.

Please provide any additional information below.

When lazy loading excanvas.js, the onreadystatechange event is never fired
because doc.readyState already reports "complete".

Original code:
init: function(opt_doc) {
    if (/MSIE/.test(navigator.userAgent) && !window.opera) {
        var doc = opt_doc || document;
        // Create a dummy element so that IE will allow canvas elements to be
        // recognized.
        doc.createElement('canvas');
        doc.attachEvent('onreadystatechange', bind(this.init_, this, doc));
    }
},

The fix is to add a simple check to see if the readyState is already set to
"complete". If yes, invoke init_ immediately. If not, add an event handler
to invoke init_ as originally handled by excanvas

Edited code:
init: function(opt_doc) {
    if (/MSIE/.test(navigator.userAgent) && !window.opera) {
        var doc = opt_doc || document;
        // Create a dummy element so that IE will allow canvas elements to be
        // recognized.
        doc.createElement('canvas');
        if(doc.readyState !== "complete"){
            doc.attachEvent('onreadystatechange', bind(this.init_, this, doc));
        } else {
           this.init_(doc);
        }
    }
},

excanvas works great, thanks for your hard work!

Original issue reported on code.google.com by platelu...@gmail.com on 20 May 2010 at 8:17

GoogleCodeExporter commented 9 years ago
I encountered the same fix on this site here that actually indicated it was 
already put into v3, so I was suprised that it wasn't in the google code 
version:

http://pipwerks.com/2009/03/12/lazy-loading-excanvasjs/

This seems like a very simple and harmless fix - what will it take to implement 
it? Does Google Code have a pull request type of thing?

Original comment by thedaily...@gmail.com on 21 May 2012 at 9:22

GoogleCodeExporter commented 9 years ago
A different workaround for this issue: 
http://badsyntax.co/post/lazyload-excanvas

Original comment by willis...@gmail.com on 24 Jan 2013 at 4:17