Help needed for ExecuteJavaScript

I want to use the following snippet in a WE app. I have tried entering that into a Page Source but it does not execute.

I know how to use ExecuteJavaScript for single line statements and even put them on the same line when they end with a semi colon but here I am lost to enter a function declaration such as displaywheel which spans 5 lines.

[code]

Wheel Delta value:

[/code]

Can I enter the if elseif structure as below, as I would do in Perl ? :

ExecuteJavaScript("if (document.attachEvent) { document.attachEvent(""on""+mousewheelevt, displaywheel)} else if (document.addEventListener) {document.addEventListener(mousewheelevt, displaywheel, false)} ")

Please forgive the naivet of these questions. I tried to experiment by placing each line in an ExecuteJavaScript("") structure but it does not execute. I then proceeded by simply sticking together the lines, but I got an unexpected token error. There must be some secret I need to learn.

Any help will be greatly appreciated.

Thank you in advance.

The issue is that executejavascript runs your code in a sort of sandbox, so after it executes, it goes out of scope and disappears. You’d be better off wrapping this code in a WebControlWrapper using the WebSDK.

Apparently, unlike what happens for two shell, variables declared in one ExecuteJavaScript are accessible in a subsequent one. So it seems they do not disappear.

I have opened WebControlSDK.pdf section 4 and will read it. At first glance, I see no example of how to execute multi-line JavaScript code, though.

Maybe I can use Xojo.loadScript(controlID As String, URL As String, JSCallback As Function, Location As String, Source As String) ? If I understand right, it seems to be able to run code locally.

Thank you.

1 Like

I just saw the light. I placed the snippet into a text file (onMouseWheel.js), dragged it into the project, then went :

ExecuteJavaScript(onMouseWheel)

It works like a charm :slight_smile:

So to answer my concern of executing multi line JavaScript code in a single ExecuteJavaScript statement, simply separate the lines with EndOfLIne within a string.

I will mark that as answer. I am sure I am not the only one wanting to use JavaScript snippets !