Xojo and Javascript

I have no experience with Javascript I try to explain to my students how to access information on a webpage from a Xojo desktop app.

I have an input field on a webpage referenced with id = mt_organizationno". The page is open and available with HTMLViever1 (Class DesktopHTMLViewer). How can I get the information from the input field? This was my guess:

Var titlePage As String  = htmlviewer1.ExecuteJavaScriptSync("document.title")
MessageBox(titlePage)

Var organisationNumber As String htmlviewer1.ExecuteJavaScriptSync("document.GetElementById(""mt_organizationno"").Value")
Messagebox(organisationNumber)

The title is displayed correctly. However, the variable organisationNumber is empty. Is there an error in the Javascript sentence?

do not mix upper lower case names. Methods are camelCase

if you open/view this in firefox browser, see console output.

test.html

<html>
<body>
<input type='text' id='a' value='Hello'>
<script>  
test();
function test() {
 var element=document.getElementById('a');
  console.log(element.value);	
} 
</script>
</body>
</html>

I changed getElementById in Camel case but the result is still empty. Yes, your javascript program works well. Is it necessary to put the Javascript function in a separate string?

have you changed it to lower case too?

.value

Thank you, now it’s perfect!

Var organisationNumber As String = htmlviewer1.ExecuteJavaScriptSync("document.getElementById(""mt_organizationno"").value;")
Messagebox(organisationNumber)
1 Like