Interacting With JavaScript using Browser widget
Posted: Wed Nov 07, 2018 4:59 pm
In the Guide in the Dictionary it says:
"The browser allows two-way interaction with the javascript running in the displayed page. To allow LiveCode handlers to be called from JavaScript, there is a javascriptHandlers property. This can be set to a list of LiveCode handlers which appear as methods attached to a global liveCode object. You can call these methods as you would any other JavaScript function and pass whatever parameters you require."
In the API under javascriptHandlers it gives an example:
-- Define a handler to respond to javascript calls.
on myJSHandler pMessage, pValue
-- Do appropriate actions here.
-- ...
end myJSHandler
-- Set up the browser javascript handler list
-- This code goes in a suitable setup handler
set the javascriptHandlers to "myJSHandler" & return & "myOtherJSHandler"
// Calling the handler from JavaScript within the browser
liveCode.myJSHandler("myMessage", 12345);
I have set up the javascriptHandlers property in the card script containing the Browser widget:
on hJS_wordCount pNumber
set label of button "wordCount" to pNumber
end hJS_wordCount
set the javascriptHandlers to "hJS_wordCount"
However, the JavaScript in the web page being displayed does not seem to be able to find 'a global liveCode object'. It is being called like this:
liveCode.hJS_wordCount(myDirection.length);
But the call is not being processed and the 'global liveCode object' appears to be undefined.
How do we define/instantiate this global liveCode object in order to access its methods?
Thanks in advance.
"The browser allows two-way interaction with the javascript running in the displayed page. To allow LiveCode handlers to be called from JavaScript, there is a javascriptHandlers property. This can be set to a list of LiveCode handlers which appear as methods attached to a global liveCode object. You can call these methods as you would any other JavaScript function and pass whatever parameters you require."
In the API under javascriptHandlers it gives an example:
-- Define a handler to respond to javascript calls.
on myJSHandler pMessage, pValue
-- Do appropriate actions here.
-- ...
end myJSHandler
-- Set up the browser javascript handler list
-- This code goes in a suitable setup handler
set the javascriptHandlers to "myJSHandler" & return & "myOtherJSHandler"
// Calling the handler from JavaScript within the browser
liveCode.myJSHandler("myMessage", 12345);
I have set up the javascriptHandlers property in the card script containing the Browser widget:
on hJS_wordCount pNumber
set label of button "wordCount" to pNumber
end hJS_wordCount
set the javascriptHandlers to "hJS_wordCount"
However, the JavaScript in the web page being displayed does not seem to be able to find 'a global liveCode object'. It is being called like this:
liveCode.hJS_wordCount(myDirection.length);
But the call is not being processed and the 'global liveCode object' appears to be undefined.
How do we define/instantiate this global liveCode object in order to access its methods?
Thanks in advance.