"getValue is not a function"?

bostonquad shared this question 4 years ago
Needs Answer

What am I missing when it comes to calling applet functions from JavaScript?


ggb file attached; HTML below:


<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript" src="https://cdn.geogebra.org/ap...;

</script>

<script type="text/javascript">

var applet = new GGBApplet({filename: "bare.ggb"});

window.onload = function() {

applet.inject('applet_container');

alert(applet.getValue("happy")); // error at this line

}

</script>

</head>

<body>

<div id="applet_container"></div>

</body>

</html>

Files: bare.ggb
Best Answer
photo

I figured it out. There are 2 problems in my HTML code:


1) The GGBApplet object I'm referring to is not the object that has the methods I want to call. (They're clearly related, but I'm not sure how.) The object I'm interested in will be an element of the document, and by default has the name ggbApplet.

2) The javascript is executing asynchronously, and the applet hasn't loaded and been inserted into the document by the time I try my sample code tries to call methods on it. I have to wait for the applet to load.


One way to fix my example code is to replace

alert(applet.getValue("happy")); // error at this line

with

setTimeout(myFunction, 5000);

function myFunction() {

alert(ggbApplet.getValue('happy'));

}

Comments (3)

photo
1

I figured it out. There are 2 problems in my HTML code:


1) The GGBApplet object I'm referring to is not the object that has the methods I want to call. (They're clearly related, but I'm not sure how.) The object I'm interested in will be an element of the document, and by default has the name ggbApplet.

2) The javascript is executing asynchronously, and the applet hasn't loaded and been inserted into the document by the time I try my sample code tries to call methods on it. I have to wait for the applet to load.


One way to fix my example code is to replace

alert(applet.getValue("happy")); // error at this line

with

setTimeout(myFunction, 5000);

function myFunction() {

alert(ggbApplet.getValue('happy'));

}

photo
1

Try using ggbOnInit()


https://wiki.geogebra.org/e...

photo
1

Thank you. I'm unclear on ggbOnInit(), though. I have some questions.


1. Should this code work in Global JavaScript? It doesn't seem to do anything.


function ggbOnInit() {

alert("test");

}


2. If I have a syntax error in ggbOnInit(), how is that reported to me? When I introduce a syntax error, I see no message indicating the error.


3. Can I use ggbOnInit() to call JavaScript defined externally (such as a JavaScript function defined in the HTML file that's embedding the ggb)? I don't doubt this is possible, but I'd like to check.

photo
© 2021 International GeoGebra Institute