javascriptgeogebra

'ggbApplet is not defined' error in javascript


I have a web page which include a geogebra applet. On loading the page I would like to modify certain elements in the applet, depending on user input. I tested the following code

 <script type="text/javascript">
 var parameters = {
   "width":600,"height":450,
   "filename":"trois_ensembles.ggb"};

var applet = new GGBApplet('5.0', parameters);

window.onload = function() {
    applet.inject('applet_container');
    ggbApplet.setVisible('A', false);
}
</script>

But the line

ggbApplet.setVisible('A', false);

produces an error in the console

ReferenceError: ggbApplet is not defined

When ggbApplet is referenced elsewhere, as in

   <button onclick="ggbApplet.setVisible('A', true)">Set A </button>

everything works as expected.

I don't understand the scope of ggbApplet. Help appreciated.


Solution

  • Checking Global JavaScript section of Scripting page of Geogebra's wiki may be useful.

    Cheers.

    See this section:

    Note: Using any ggbApplet methods in Global JavaScript outside of ggbOnInit will not work as intended since they will be called before the construction is loaded.