Chris
Chris

Reputation: 13

Function "is not defined"

I have this code

        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
        google.load("maps", "3",  {other_params:"sensor=false"});
        google.load("jquery", "1.3.2");
        google.load("visualization", "1", {packages: ["columnchart"]});

        function initialize() {

            // some actions...

            function mapload(myfile) {
                    jQuery.get("trace_" + myfile + ".xml", {}, function(data) {
                        // some actions...
                    });
            }

            mapload('ah');

        }

        google.setOnLoadCallback(initialize);
        </script>

        <input type="button" value="Hunt Mesa" onclick="mapload('hunt')" />

The first "mapload" works fine

But the onclick button say "mapload is not defined".

Do you know why ?

Upvotes: 1

Views: 406

Answers (1)

MvanGeest
MvanGeest

Reputation: 9661

mapload() is only defined within initialize(). When your onclick handler tries to call it, it doesn't exist any more. To solve your problem, a quick&dirty solution is to replace all occurrences of mapload with window.mapload. So write

        window.mapload = function (myfile) {
                jQuery.get("trace_" + myfile + ".xml", {}, function(data) {
                    // some actions...
                });
        }

and

<input type="button" value="Hunt Mesa" onclick="window.mapload('hunt')" />

Upvotes: 2

Related Questions