Interacting With JavaScript using Browser widget
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 15
- Joined: Sat Mar 20, 2010 6:03 pm
Interacting With JavaScript using Browser widget
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.
Re: Interacting With JavaScript using Browser widget
Hi Mark,
should do the trick
Best
Klaus
Code: Select all
set the javascriptHandlers OF WIDGET "your browser widget name here!" to ...
Best
Klaus
-
- Posts: 15
- Joined: Sat Mar 20, 2010 6:03 pm
Re: Interacting With JavaScript using Browser widget
Thanks Klaus,
My card script now has:
set the javascriptHandlers of widget "Browser" to "hJS_wordCount"
But the JavaScript in the web page still isn't managing to call the hJS_wordCount handler.
Any more thoughts?
My card script now has:
set the javascriptHandlers of widget "Browser" to "hJS_wordCount"
But the JavaScript in the web page still isn't managing to call the hJS_wordCount handler.
Any more thoughts?
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Interacting With JavaScript using Browser widget
Your settings are correct.
You could try
If this works then your javascript/htmltext is the problem.
You could try
Code: Select all
do "liveCode.hJS_wordCount(1234);" in widget "Browser"
shiftLock happens
-
- Posts: 15
- Joined: Sat Mar 20, 2010 6:03 pm
Re: Interacting With JavaScript using Browser widget
Thanks -hh,
After playing around with things, it's now working.
I used Klaus's suggestion of set the javascriptHandlers of widget "Browser" to "hJS_wordCount"
I then used the sample LiveCode stack provided by LCNeil in the thread: "Passing arguments to Javascript from Livecode"
This uses a stack custom property "cHTML" to store the HTML. After establishing a JavaScript connection using that method, when I reverted to a remotely loaded URL, and it just worked.
After playing around with things, it's now working.
I used Klaus's suggestion of set the javascriptHandlers of widget "Browser" to "hJS_wordCount"
I then used the sample LiveCode stack provided by LCNeil in the thread: "Passing arguments to Javascript from Livecode"
This uses a stack custom property "cHTML" to store the HTML. After establishing a JavaScript connection using that method, when I reverted to a remotely loaded URL, and it just worked.