user796443
user796443

Reputation:

Jquery Cannot call method 'load' of undefined

I'm using jquery to load google maps after button toggle:

function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://maps.googleapis.com/maps/api/js?v=3.11&key=AIzaSyCK3XUMWOH0GIiuj3VeprakKZXoo_nDV08&sensor=true&' +
        'callback=initialize';
    document.body.appendChild(script);
}

(function ($) {
    $(document).ready(function () {
        //Show map on category pages
        $(function () {
            $("#showonmap a").toggle(function () {
                $(this).children("#show_map").hide();
                $(this).children("#hide_map").show();
                //Reveal google maps multi
                //First create empty divs, if they exist don't create them
                if ($("#googlemap_multi").length == 0) {
                    $("#subheadergradient").before('<div class="googlemap_margin"></div><div id="googlemap_multi"></div><div class="googlemap_margin"></div>');
                }
                $(".googlemap_margin").fadeIn("slow", function () {
                    $("#googlemap_multi").animate({
                        height: 400
                    }, "slow", function () {
                        //load google maps
                        loadScript().load();
                    });
                });
            }, function () {
                $(this).children("#hide_map").hide();
                $(this).children("#show_map").show();
                //Hide google maps multi
                $("#googlemap_multi").animate({
                    height: 0
                }, "slow", function () {
                    $(".googlemap_margin").fadeOut("slow");
                });
            });
        });
    });
}(jQuery));

But this generates error:

Uncaught TypeError: Cannot call method 'load' of undefined localhost:482
(anonymous function) localhost:482
d.complete jquery.min.js:4
f.fx.step jquery.min.js:4
h jquery.min.js:4
f.extend.tick

So toggle back function never gets executed.

This part:

//Hide google maps multi
$("#googlemap_multi").animate({height: 0},"slow", function(){
    $(".googlemap_margin").fadeOut("slow");
});

What is the problem?

ty.

Upvotes: 0

Views: 1517

Answers (3)

Grzegorz Kaczan
Grzegorz Kaczan

Reputation: 21694

Why do you try to call .load() on a function that do not returns nothing?

Either just call loadScript(); or or make loadScript function return an object that has method load.

Upvotes: 1

S&#233;bastien Saunier
S&#233;bastien Saunier

Reputation: 1877

You are calling

loadScript().load();

But your method loadScript doesn't have any return statement, so basically you are calling the load method (chained) on undefined.

Upvotes: 1

Rudie
Rudie

Reputation: 53890

The result of loadScript() doesn't have a method load(). All it does is load a script (async!).

Which load() are you trying to execute?

Upvotes: 1

Related Questions