1

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 ?

1 Answer 1

2

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')" />
2
  • OK MvanGeest... so how can I call "mapload" from the onclick button ?
    – Chris
    Jun 19, 2010 at 12:27
  • Can you accept the answer (that will be possible in 8 minutes)? No thanks, I had to read a good JS book before I could avoid such problems.
    – MvanGeest
    Jun 19, 2010 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.