简体   繁体   中英

Getting 'jQuery not defined' error in my widget library

What I am doing

I am creating a distributable jQuery widget library which inserts an anchor on HTML page.

Issue

In 'MyWidget.prototype.Render' method (of JS), its giving me following error - "jQuery is not defined" However, its working fine in 'MyWidget.prototype.ResolveJqueryAndLoadAdditionalJsAndCss' method.

HTML Snippet

<html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <a href="http://yahoo.com">Anchor - by user code</a>

    <!-- Widget Script: Starts -->
    <script type="text/javascript" src="widget.js" ></script>
    <Script type="text/javascript"> 
        var customWidget = new MyWidget();
        customWidget.Render({ anchorText:'Hola!!!' });
    </script>
    <!-- Widget Script: Ends -->
</body>
</html>

JS Code(widget.js)

    var MyWidget = function() {
    // Localize jQuery variable
    var jQuery;
    // Load jQuery if not present
    if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
        var script_tag = document.createElement('script');
        script_tag.setAttribute("type","text/javascript");
        script_tag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js");
        if (script_tag.readyState) {
        script_tag.onreadystatechange = function () { // For old versions of IE
            if (this.readyState == 'complete' || this.readyState == 'loaded') {
                this.ResolveJqueryAndLoadAdditionalJsAndCss();
            }
        };
        } else {
            script_tag.onload = this.ResolveJqueryAndLoadAdditionalJsAndCss;
        }
        // Try to find the head, otherwise default to the documentElement
        (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
    } else {
        // The jQuery version on the window is the one we want to use
        jQuery = window.jQuery;
        this.ResolveJqueryAndLoadAdditionalJsAndCss();
    }
    };

MyWidget.prototype.ResolveJqueryAndLoadAdditionalJsAndCss = function() {
        // Restore $ and window.jQuery to their previous values and store the
        // new jQuery in our local jQuery variable
        jQuery = window.jQuery.noConflict(true);
        jQuery(document).ready(function($) {
            $.when(
                $.getScript( "http://www.jacklmoore.com/colorbox/jquery.colorbox.js" ),
                $.Deferred(function( deferred ){
                    $( deferred.resolve );
                })
            ).done(function(){ });
            // Loading Custom CSS
            var css_link = $("<link>", { rel: "stylesheet", type: "text/css", href: "https://raw.github.com/premasagar/cleanslate/master/cleanslate.css" });
            css_link.appendTo('head');
            css_link = $("<link>", { rel: "stylesheet", type: "text/css", href: "http://www.jacklmoore.com/colorbox/example4/colorbox.css" });
            css_link.appendTo('head');
            css_link = $("<link>", { rel: "stylesheet", type: "text/css", href: "widget.css" });
            css_link.appendTo('head');
        });
    };

MyWidget.prototype.Render = function(data){
        jQuery(document).ready(function($) {
            $('<a href="http://wikipedia.com" class="cleanslate custom widgetExternalPage">' + data.anchorText + '</a>').appendTo('body');
            $(".widgetExternalPage").colorbox({iframe:true, width:"80%", height:"80%"});
        });
    };

well, you are re-defining jQuery . Actually, you are re-declaring any variable is also re-setting the value of it. Here, you are setting the value of jQuery to undefined . You should not do this.

If you want, you can do it like:

var jQuery = jQuery || undefined;

This may help, good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM