gwatts / jquery.sparkline

A plugin for the jQuery javascript library to generate small sparkline charts directly in the browser
http://omnipotent.net/jquery.sparkline/
1.24k stars 278 forks source link

Undefined error in IE 8 related to document.namespaces #64

Closed timtucker closed 11 years ago

timtucker commented 11 years ago

I'm seeing an "Unspecified error" in IE 8 sporadically when loading.

The section of code flagged looks to be the VML code at: if (document.namespaces && !document.namespaces.v) { $.fn.sparkline.hasVML = true; document.namespaces.add('v', 'urn:schemas-microsoft-com:vml', '#default#VML'); } else { $.fn.sparkline.hasVML = false; }

The behavior seems pretty consistent with what I see described here: http://drupal.org/node/613002

One fix that I can see from what's described above would be to make hasVML a function instead of a boolean and call it when needed (potentially caching the result after the first call). That way it's less likely to get called before document.ready / document.load.

timtucker commented 11 years ago

Using the following replacement for $.fn.simpledraw could work (along with removing the whole setup for hasCanvas / hasVML):

$.fn.simpledraw = function (width, height, useExisting, interact) {
        var target, mhandler;
        if (useExisting && (target = this.data('_jqs_vcanvas'))) {
            return target;
        }

        if ($.fn.sparkline.canvasFunction === false)
        {
            // We've already determined that neither Canvas nor VML are available
            return false;
        }
        else if ($.fn.sparkline.canvasFunction === undefined)
        {
            // No function defined yet -- need to see if we support Canvas or VML
            var el = document.createElement('canvas');
            if (!!(el.getContext && el.getContext('2d')))
            {
                // Canvas is available
                $.fn.sparkline.canvasFunction = function(width, height, target, interact) {
                    return new VCanvas_canvas(width, height, target, interact);
                };
            }
            else if (document.namespaces && !document.namespaces.v) {
                // VML is available
                document.namespaces.add('v', 'urn:schemas-microsoft-com:vml', '#default#VML');
                $.fn.sparkline.canvasFunction = function(width, height, target, interact) {
                    return new VCanvas_vml(width, height, target);
                };
            }
            else
            {
                // Neither Canvas nor VML are available
                $.fn.sparkline.canvasFunction = false;
                return false;
            }
        }

        if (width === undefined) {
            width = $(this).innerWidth();
        }
        if (height === undefined) {
            height = $(this).innerHeight();
        }

        target = $.fn.sparkline.canvasFunction(width, height, this, interact);

        mhandler = $(this).data('_jqs_mhandler');
        if (mhandler) {
            mhandler.registerCanvas(target);
        }
        return target;
    };
gwatts commented 11 years ago

Good catch - I think your suggestion looks good - if you can submit a pull request with your change I'll merge it in

timtucker commented 11 years ago

Pull request submitted. I shortened in the final code from $.fn.sparkline.canvasFunction to $.fn.sparkline.canvas to make the code size slightly smaller.

gwatts commented 11 years ago

Cherry-picked and merged PR https://github.com/gwatts/jquery.sparkline/pull/66 as commit e8c4595a6a209dc1416177828d69d08c1b59fd2f