Should changing an HTMLViewer's window.status work in Windows?

I assume so, since that seems to be the way everyone passes data from javascript to Xojo.

In my app, everything works great on Mac, but on Windows, it ignores any mouse clicks. I’m using the Webkit renderer in both places. Appearance-wise, they both look and behave identically, I just can’t get the Windows version to acknowledge any clicks.

I found if you are changing the window.status or the document.title and you are changing it to the same value, the events do not get fired in Xojo…

For example, if you do a document.title = ‘click’; and then set the title to ‘click’ again, the .TitleChanged event only fires once… I have been adding a space and then String(Math.random()); to cause the event to always fire… (just check the first NthField of the newTitle/newStatus parameter to get the event name)…

If you are looking at getting data from JavaScript back to the HTMLViewer, you could always sub-class the HTMLViewer and add the following method

[code]Function EvaluateJavaScript(script As String) As String
DIM returnValue As String = “”

#if TargetMacOS
Declare Function stringByEvaluatingJavaScriptFromString Lib “Cocoa” Selector “stringByEvaluatingJavaScriptFromString:” (id As Integer, script As CFStringRef) As CFStringRef
returnValue = stringByEvaluatingJavaScriptFromString(Me.Handle, script)
#endif

Return returnValue
End Function[/code]

If you want to add it to a module and just extend the built-in HTMLViewer without sub-classing then use the following version

[code]Function EvaluateJavaScript(Extends viewer As HTMLViewer, script As String) As String
DIM returnValue As String = “”

#if TargetMacOS
Declare Function stringByEvaluatingJavaScriptFromString Lib “Cocoa” Selector “stringByEvaluatingJavaScriptFromString:” (id As Integer, script As CFStringRef) As CFStringRef
returnValue = stringByEvaluatingJavaScriptFromString(viewer.Handle, script)
#endif

Return returnValue
End Function[/code]

What’s wrong with ExecuteJavascript?

It looks like shao sean’s methods return the value from the javascript, while the built in ExecuteJavascript does not.
Would be a nice to see built in.

Shao’s method for Mac OS X looks good.

[quote=117524:@Tim Parnell]It looks like shao sean’s methods return the value from the javascript, while the built in ExecuteJavascript does not.
Would be a nice to see built in.[/quote]

If it was built-in that would be so awesome…

Indeed. And cross platform. Maybe time to log a Feature Request …

I have not had time to test your method yet. How do you pass the variable from the JavaScript side ?

[quote=117559:@Michel Bujardet]Indeed. And cross platform. Maybe time to log a Feature Request …

I have not had time to test your method yet. How do you pass the variable from the JavaScript side ?[/quote]
stringByEvaluatingJavaScriptFromString: returns a string so it’s automagic.

So using the functions from above:

dim returnedResult as String = EvaluateJavaScript("javascriptFunctionOnTheHTMLPageThatIKnowIsGoingToReturnSomething()")

That’s very slick, but I need a cross platform solution, unfortunately.

I don’t think my issue is that the same event is being sent repeatedly though. My problem is that while the window.status trick works on mac, it seems not to on Windows.

Some more detail:

I’m using this javascript in my HTMLViewer:

Then in the StatusChanged event handler:

system.DebugLog("Got: " + newStatus)

On Mac, no problem. On Windows, nothing. Both using Webkit.

So frustrating.

[quote=117559:@Michel Bujardet]Indeed. And cross platform. Maybe time to log a Feature Request …

I have not had time to test your method yet. How do you pass the variable from the JavaScript side ?[/quote]

Cross platform, you can pass variables through the TitleChanged event. Here is a test

Sub Action() dim j as string j = "var x = ""toto"";"+EndOfLine j = j+"document.title = x" HTMLViewer1.ExecuteJavaScript(j) End Sub

When the button is clicked, the TitleChanged event is triggered and whatever document.title has been set to is in newTitle.

To prevent the user to be surprised by strange titles, use the same code to reinstate the original title right after getting the value.

Please note Shao Sean remark about calling twice with the same value to avoid not seing a return value :

[quote=117502:@shao sean]I found if you are changing the window.status or the document.title and you are changing it to the same value, the events do not get fired in Xojo…

For example, if you do a document.title = ‘click’; and then set the title to ‘click’ again, the .TitleChanged event only fires once… I have been adding a space and then String(Math.random()); to cause the event to always fire… (just check the first NthField of the newTitle/newStatus parameter to get the event name)…[/quote]

Thanks, but that’s not the problem I’m trying to solve.

In my case, everything works fine on Mac, but on Windows, the onClick inside my html isn’t generating a result.

Maybe there’s some subtle difference in how you need to code things for Windows Webkit?

[quote=117766:@Chris Kohout]Thanks, but that’s not the problem I’m trying to solve.

In my case, everything works fine on Mac, but on Windows, the onClick inside my html isn’t generating a result.

Maybe there’s some subtle difference in how you need to code things for Windows Webkit?[/quote]

My suggestion was to use TitleChanged instead of StatusChanged. But it could be the way you handle onClick that Windows HTMLViewer does not understand.

Care to post the code ?

Oh, I see. Yeah, I had tried using TitleChanged instead of StatusChanged, but no difference. I think because the click isn’t triggering the javascript at all, which is upriver of the Xojo events.

Besides the code I posted above, I’m just doing the standard onClick like this:

Click me

And loadItem is this:

[quote=117696:@Chris Kohout]I’m using this javascript in my HTMLViewer:

[/quote]

I am sorry, but I do not understand what you do with function loadItem (msg). I tried to look “JavaScript loadItem” on the Internet but it seems unknown.

Whatever that function is supposed to do, setting the status works. This does trigger the StatusChanged event fine :

Sub Action() dim j as string j = "var msg = ""toto"";"+EndOfLine j = j+"{ window.status = msg }" HTMLViewer1.ExecuteJavaScript(j) End Sub

You mentioned it seemed JavaScript was not getting click events. Here is the code I used, and it does work pereftly to detect a mouse click in the HTMLViewer (even if it is probably better to use the Xojo event) :

HTMLViewer1.ExecuteJavaScript("window.onclick = function() {alert('click!')}")

There is a catch, though. In the open event of HTMLViewer or the window it does nothing. It seems the function must be created just a while after. I obtained a result by placing the code in the Action event of a timer with a period of 500.

So it seems JavaScript functions must be created after the open event for them to exist.

WebKit renderer, Windows 8.1 64 bits Pro, IE 11.

It’s onclick no capital on the “c”

[code]

Click me

I will be attempting to do a cross-platform version of the EvaluateJavaScript, just need to figure it out…

DIM innerText As String = me.EvaluateJavaScript("document.getElementById('input').innerText;")
DIM innerText As String = me.EvaluateJavaScript("document.getElementById('input').innerText;")

Allright. Thank you :slight_smile:

Yeah it would be really nice if the HTMLViewer supported pushing content out without having to hack the title change event. Perhaps an event that is triggered when we use javascript to write to the page’s console?

There might be a feedback request for this already. I know I need it and have had to hack around this before too.

I’m looking at how to do it on the other two platforms… Easy on Cocoa as Apple has that function in the WebKit framework, but on the other platforms, it requires a few more declares to get down to the JavaScriptCore library…