ExecuteJavascript error

  1. 4 months ago

    Mathias M

    is not verified Jul 14 Pre-Release Testers

    Hello

    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.

    HTMLEditor.ExecuteJavaScript("gEditor.ProcessXojoMessages(" + Chr(34) + "blablabla" + Chr(34) + ")")

    Here I get "gEditor is not defined". What could be the issue here?

    Thanks for pointing me in the right direction.

    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.
  2. Tim P

    Jul 14 Pre-Release Testers, Xojo Pro ⭐️

    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.

  3. Mathias M

    is not verified Jul 15 Pre-Release Testers
    Edited 4 months ago

    Hi @Tim P Thanks for your answer.

    There are not really third parties involved, but I tried to choose a better title but it seems that I cannot change my first post.

    I recreated the most minimal version I could of this problem so it would be clear what problem I'm facing.
    First of all, see this page: http://conservatorium-brugge.be/xojo/javascript/
    It's a simple button which calls a function on a instance of a class, the class, instance and the creation of the instance are done with this script: http://conservatorium-brugge.be/xojo/javascript/test.js
    The JavaScript might be a little weird, but that's because it is transpiled from TypeScript, original typescript is here: http://conservatorium-brugge.be/xojo/javascript/test.ts

    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.

    I could found nothing about this in the docs.

    Thanks everybody

  4. Eli O

    is not verified Jul 15 Answer Europe (Berlin, Germany)

    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.
  5. Mathias M

    is not verified Jul 15 Pre-Release Testers
    Edited 4 months ago

    Aha, that's too bad. Both my scripts run on localhost, but on a different port, a webserver for my files :8081 and Xojo standalone on :8080.

    Will have to look into CGI to get them on the same domain I guess. It would be really helpful if this would work: https://forum.xojo.com/41840-add-folderitems-to-host-in-standalone/p1#p340158 but there seems to be no way to use the Xojo standalone is a webserver to host other files than the webapp itself.

    Edit: Even better than CGI, I can use this technique: https://en.wikipedia.org/wiki/Web_Messaging

    Thanks for your help!

  6. Greg O

    Jul 15 Xojo Inc Somewhere near Raleigh, NC

    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.

  7. Mathias M

    is not verified Jul 15 Pre-Release Testers

    @Greg OLone 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.

    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.

  8. Greg O

    Jul 17 Xojo Inc Somewhere near Raleigh, NC

    @Mathias M 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.

    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.

or Sign Up to reply!