Web 2.0 : javascript returning data to WebSession

What’s the current best practice for a Xojo Web (2.0) app to return data to the server? Specifically, I’d like to be able to use

WebSession.executeJavascript("...")

and have the result come back into the WebSession instance.

I see several threads about this, but some of them are a mix of Web 1 and Web 2, and some of them contain ‘do it this way’ with later comments saying ‘don’t do it this way’.

E.g. I see recommendations saying:

this.triggerServerEvent()
  or
XojoWeb.session.comm.triggerServerEvent()
  or
comm.triggerServerEvent()

Other suggestions are to use the WebSDK which looks more complicated than I want, as the WebSDK would requires me to add a new Control to every WebPage.

I just want the data to be returned to the WebSession instance.

Is this posible?

I don’t think there is a specific API for that, so this is a bit hacky, undocumented and could change in the future… but… if you want to avoid the Web SDK, maybe you can play with WebSession’s RecordData, RemoveData and RequestData.

The JS way to use “RecordData” is by calling XojoWeb.session.storage.setItem in JavaScript. So, for example, getting the current time:

ExecuteJavaScript("XojoWeb.session.storage.setItem('foo', new Date().getTime())")
Session.RequestData("foo")

You will receive a RequestedData event with foo as the key, and the current time from the browser.

3 Likes

That seems to work, thank you.

I’ve submitted a feature request for a more explicit event, whichi might make this a bit less hacky… https://tracker.xojo.com/xojoinc/xojo/-/issues/74033

1 Like

Thanks for the Feature Request!

@Mike_D -

There’s a technique that I shared late last year that might give you some ideas on how to return data to the correct Web session. Essentially, it involves sending requests back to the app, and the requests get handled via the HandleURL event handler. The request includes the user’s session ID, which is used to get a WebSessionContext. From there, you can pretty much do what you want.

I wrote about the technique here: Xojo: Web 2.0 Code Injection Technique and the project can be downloaded here: https://tdietrich-opensource.s3.amazonaws.com/Xojo-CodeInjector-20221215.zip

I hope this helps.

7 Likes

@Tim_Dietrich thanks!

I found another way of doing it which is super duper hacky:

Running this code in the Session:
self.ExecuteJavaScript("syntaxError12345678")

Will fail, and trigger WebSession.JavaScriptError() event. The error is
Can't find variable: syntaxError12345678

From this, you could extract the 12345678 from the error string.

I do not recommend using this for real.

However, since Xojo already has the code to send back the JavaScriptError() event, it seems likely that it wouldn’t be a large change to send back some sort of JavascriptResults() event in the same fashion.

3 Likes

Thanks Mike.

Now i can throw the JavaScript results back to XOJO web. Super cool :sweat_smile:

dim t as string
t = "function myFunction(p1, p2) {" + _
" return p1 * p2;} " + _
" throw ""the results are:"" + myFunction(12,3) ;"

me.ExecuteJavaScript t

Moral of the story, we need a more simplistic way to extend xojo web. It looks like the web 1.0 way of extending controls etc was much easier.

Back to topic, it’s interesting to read these solutions.

The concept of WebStyle still exists. If you don’t need to modify your bootstrap theme, you can modify controls individually or use a common webstyle to make them consistent.

@Tim_Hare i didn’t mean webstyle i meant the way of extednding it, sorry about that.
I’ve edited my response above.