javascript variables

Not even sure if the SDK is necessary for this, but I’m trying to create a non-interface control that sets .document variables via javascript and is able to retrieve them. I’m having a difficult time on how to simply retrieve a javascript variable and pass it into a variable in app. Anybody have any samples?

Nevermind… got a solution working. I put this in the app.htmlheader section:

<script type="text/javascript"> function importVariable(webTextElement, referenceValue) { document.getElementById(webTextElement).value = referenceValue; } </script>

then I can use this in a method;

[code]dim javaSource as string

javaSource = “importVariable(’” + textfield1.ControlID + “_inner’, document.title);” // - replace document.title with variable
excuteJavaScript(javaSource)[/code]

Actually, that doesn’t work… it fills the textfield with the appropriate value, but it does it client-side and doesn’t fire a textchanged event to bring the data back to the server end. Still working on a solution, so help is appreciated.

Try something along this

Xojo.get('"+textfield1.ControlID+"').value = 'your new value';

Don’t know though if this triggers the changed event (since there’s no user action involved. I’d expect it to though)

If it doesn’t, have a look at “Xojo.TriggerBrowserEvent”.

If you look at the Xojo.get function, you’ll notice it’s simply a document.getElementById return with no other triggers. Also, I don’t think the ‘Xojo.TriggerBroswerEvent’ is it. The textcontrol script calls:

markControlChanged(this);
Xojo.comm.triggerEvent(this.controlID,‘TextChanged’);

after events… but calls of variations of those after the value was updated hasn’t been successful for me yet. I’m not giving up though…

I know that it’s an alias. I actually wanted to point jo towards the .value property rather than altering the .inner/.innerHTML… Value is the property of an html input tag - thus you should rather change that.

Eric, don’t use the internal method. There’s one in the sdk, named TriggerServerEvent, just for this purpose. The undocumented one is subject to change without warning.

Thanks Greg, I found it a little while after posting this and am using the one in the SDK… with success. Appreciate it!

Alright, found a solution to pull variable information from the framework and importing it into Xojo without editing the framework.js (or any framework files) or using the WebSDK, instead using a subclass of the webTextField. I’m attaching the control here for download. To use:

Example: ioField1.javaVariable(“Xojo.version”, true)

returns the value of ‘Xojo.version’ within the framework. The ‘true’ means it will fire a TextChanged event once the text has updated with the variable information. Set to ‘false’ if you don’t want anything to fire.

Some uses for it… you can use it for things like:

ioField1.javaVariable(“sessionStorage.getItem(‘mykey’)”, false) to have it return a DOM sessionStorage or localStorage value that you set earlier on webPage.open event using ‘me.executejavascript("sessionStorage.setItem(‘mykey’, ‘Some Value’)’. You can also set the visibility to the ioField to false if you just want to use it as a retrieval container to pull data out of (and not actually show on the page).

Test it out, let me know of any issues…

NOTE: Use cautiously, as it will throw a javascript error on undefined variables or syntax errors (will suppress the error dialog on undefined variables later).

https://dl.dropboxusercontent.com/u/13574877/ioField.xojo_binary_code

While looking into getting cookies back from the HTMLViewer, I just found a new way to return the JavaScript variable to Xojo without messing up the page title, which appears in Chrome or Internet explorer.

Instead of using the Titlechange event, i use the CancelLoad one.

Here is an example :

In a button, the code that gets cookies :

Sub Action() HTMLViewer1.ExecuteJavaScript("window.location='jsreturn'+document.cookie;") End Sub

Note the “jsreturn” token at the beginning, which will be used to distinguish between regular URLs and a JavaScript variable.

In the CancelLoad event, we look if the token is present. If so, the URL is decoded, its encoding defined, then split with “jsreturn”, and finally available in the array jsreturn(1). Used here in a MsgBox.

Function CancelLoad(URL as String) As Boolean if instr(URL,"jsreturn") > 0 then dim theURLString as string theURLString = DefineEncoding(DecodeURLComponent(URL), Encodings.UTF8 ) dim jsreturn() as string = split(theURLString,"jsreturn") MsgBox Replaceall(jsreturn(1),";",EndOfLine) return true end if End Function

Must have been real tired. This should go into Desktop where I am going to copy it now. Sorry.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.