How can I read variable from javascript?

Hi,
I use complex javascript code and I need read variable: “ganttChartView.chartContainer.clientWidth” from javascript into the xojo. I tried to find something on the internet, but I found only in Real Software forum - how to read browser version trough session.cokies.value, but when I try it with my variable - it doesn’t work.
Please, what do you think what should I do?

Lets say your HTMLViewer is called HTMLViewer1. The quick way to do this would be to manipulate the title when you want to pass information from the HTMLViewer to Xojo.

The steps:

  1. For changing the value, you may want to add a setter function in javascript like this:
function setClientWidth(obj, value) {
obj.clientWidth = value;
document.title = value;
}
  1. Set the clientWidth variable (in javascript) with something like ‘setClientWidth(ganttChartView.chartContainer)’.
  2. Simply use the TitleChanged event in the HTMLViewer and parse the value from that.

You may want to structure this better and make it so you can read variables in a more powerful way but this should give you the general idea.

The technique may not seem great but it is an easy way of doing it and it should work on all platforms and there is a lot you can do to get the title to work how you want it to.

Ask me if you unsure on anything. For example, how you can use this technique with websites that you did not develop.

Hope this helps

How about searching the forum to see if this has been asked/answered already:

https://forum.xojo.com/8058-how-to-get-a-javascript-value
https://forum.xojo.com/16447-getting-a-number-from-javascript

These are just a couple from this current year. I know there are many more before that.

Thank you.
If someone read this, I did it, when I want variable(ganttChartView.chartContainer.clientWidth) from javascript:

  dim jscript as string
  jscript = "document.getElementById('" + TextField2.controlID + "_inner').value = ganttChartView.chartContainer.clientWidth;"
  textfield2.ExecuteJavaScript(jscript)

Hi, I have next problem. I have this javascript in button and there is too xojo code. When I use:

textfield2.ExecuteJavaScript(jscript)

So, it nothing happend. It insert variable into textfield2 after finish button event. I tried something as window.refresh and so on, but nothing help me.
When I tried msgbox

  jscript2 = jscript2+ "var visibleHours =ganttChartView.chartContainer.clientWidth / settings.hourWidth;"+EndOfLine
  jscript2 = jscript2+ "var visibleDays = Math.floor(visibleHours / 24);"+EndOfLine
  jscript2 = jscript2+ "document.getElementById('"+TextField2.controlID+"_inner').value=visibleDays;"+EndOfLine
  jscript2 = jscript2 +"var pokus = ganttChartView.settings.timelineFinish -   ganttChartView.settings.timelineStart;"+EndOfLine
  jscript2 = jscript2+ "document.getElementById('"+TextField3.controlID+"_inner').value=pokus;"+EndOfLine
  TextField2.ExecuteJavaScript(jscript2)
  
  msgbox TextField2.text
...
...

in msgbox is nothing, but after button comleted event. Then it set value into textfield2. Why don’t do it immediately?

[quote=142381:@Krystof Jurecka]Thank you.
If someone read this, I did it, when I want variable(ganttChartView.chartContainer.clientWidth) from javascript:

dim jscript as string jscript = "document.getElementById('" + TextField2.controlID + "_inner').value = ganttChartView.chartContainer.clientWidth;" textfield2.ExecuteJavaScript(jscript) [/quote]

This will not work since the app will not get the value of the text field you are setting by hacking the DOM.

Better set the page hashtag, and get that value through the session HashTagChanged event.

JavaScript example :

ExecuteJavaScript(" window.location.replace('#'+'mystring'); ")

Thank you Michel. But i Have with them the same problem.
in button:

ExecuteJavaScript(" window.location.replace('#'+'mystring'); ") msgbox scaledays
in hashtagchanged event in session:

webpage1.scaledays = self.HashTag

in properties:
scaledays as string

msgbox ist clear… so Do I have to have my code in hashtagchanged event? Can not be in button?

[quote=142607:@Krystof Jurecka]Thank you Michel. But i Have with them the same problem.
in button:

ExecuteJavaScript(" window.location.replace('#'+'mystring'); ") msgbox scaledays
in hashtagchanged event in session:

webpage1.scaledays = self.HashTag

in properties:
scaledays as string

msgbox ist clear… so Do I have to have my code in hashtagchanged event? Can not be in button?[/quote]

What is it you think you are doing in HashtagChanged ?

Simply place there :

MsgBox HashTag

And the Msgbox will say “mystring”.