I’m trying to execute some javascript in an WebHTMLViewer control. But I keep getting a x is not defined error. While the same code run from a button just works.
So, I have this button in the .html file:
<button onclick='gEditor.ProcessXojoMessages("blabla");'>test</button>
This works, but when I have this code on a button (so I can click it when the html is certainly loaded.
Well, I mean the issue here is that gEditor is not defined. This one will be hard to sort out without the project.
You might consider changing the thread’s title to include any third party libraries you are using to attract the author of those libraries to your thread for an answer.
So pushing that button just works, the function is called, the string is added and an alert is shown.
Now I created a webproject, with a htmlviewer and a button. The htmlviewer shows the right page, the button calls executejavascript with the same functionality as the button inside the html. You can download the webproject here: http://conservatorium-brugge.be/xojo/javascript/testjs.xojo_binary_project
As you can see, the testInstance is declared globally, it’s called on the button, but shows an error when executing javascript from within the webapp, stating that testInstance isn’t defined. It def. is, but there must be some confusion about the scope where Xojo executes this Javascript.
The “global” vars of the IFrame page are only global for the page they are defined in, but not for the parent enclosing it.
You can try to access them like this:
HTMLViewer1.ExecuteJavaScript( _
"var testInstance = document.getElementsByTagName(""IFRAME"")[0].contentWindow.testInstance;" + _
"testInstance.TestMethod(" + chr(34) + "hello" + chr(34) + ");" _
)
…but you will run into the following limitation (called “cross-domain restriction” or “same-origin policy”):
Could not execute returned javascript: Blocked a frame with origin "http://127.0.0.1:8080" from accessing a frame with origin "http://www.conservatorium-brugge.be". Protocols, domains, and ports must match.
You might also want to look at the docs for the WebSDK in the extras folder. It may help you get your libraries to the browser before your code executes.
[quote=340581:@Greg O’Lone]You can actually. Look at WebFile.
You might also want to look at the docs for the WebSDK in the extras folder. It may help you get your libraries to the browser before your code executes.[/quote]
Thanks Greg for the WebFile knowledge. This is amazing, my gut told me already that I was doing a really weird practice, actually running 2 webservers etc.
The WebSDK looks really amazing. But the library I’m using is a little to big to convert to a control I’m afraid. It’s the BabylonJS 3D Game engine. Would be amazing to address is directly, but I don’t think my JS knowledge would be good enough, and I’m afraid it would take me months to convert all methods etc.
[quote=340605:@Mathias Maes]Thanks Greg for the WebFile knowledge. This is amazing, my gut told me already that I was doing a really weird practice, actually running 2 webservers etc.
The WebSDK looks really amazing. But the library I’m using is a little to big to convert to a control I’m afraid. It’s the BabylonJS 3D Game engine. Would be amazing to address is directly, but I don’t think my JS knowledge would be good enough, and I’m afraid it would take me months to convert all methods etc.[/quote]
You don’t have to convert it, but the LoadLibraries method will call a JavaScript method of your choice when the Babylon library has made it to the page do you can initialize and start using it.