Interacting With JavaScript using Browser widget

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
markosborne
Posts: 8
Joined: Sat Mar 20, 2010 6:03 pm

Interacting With JavaScript using Browser widget

Post by markosborne » 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.

Klaus
Posts: 10384
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Interacting With JavaScript using Browser widget

Post by Klaus » Wed Nov 07, 2018 5:13 pm

Hi Mark,

Code: Select all

set the javascriptHandlers OF WIDGET "your browser widget name here!" to ...
should do the trick :D

Best

Klaus

markosborne
Posts: 8
Joined: Sat Mar 20, 2010 6:03 pm

Re: Interacting With JavaScript using Browser widget

Post by markosborne » Wed Nov 07, 2018 5:23 pm

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?

Klaus
Posts: 10384
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Interacting With JavaScript using Browser widget

Post by Klaus » Wed Nov 07, 2018 5:38 pm

No, sorry :(

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1780
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Interacting With JavaScript using Browser widget

Post by [-hh] » Wed Nov 07, 2018 8:07 pm

Your settings are correct.

You could try

Code: Select all

do "liveCode.hJS_wordCount(1234);" in widget "Browser"
If this works then your javascript/htmltext is the problem.
shiftLock happens

markosborne
Posts: 8
Joined: Sat Mar 20, 2010 6:03 pm

Re: Interacting With JavaScript using Browser widget

Post by markosborne » Thu Nov 08, 2018 6:53 pm

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.

Post Reply

Return to “Internet”