Browser widget and javascript
Tom Glod
tom at makeshyft.com
Sat Jun 28 17:17:00 EDT 2025
Sometimes triggering the callback is tricky, here is a simple working
example. The widget has a javascript handler called "ReturnPageHeight"
which receives the result in its first parameter
command getPageHeight pBrowser
local tScript
-- JavaScript code to get the page's scrollable height
put "function getScrollHeight() {" & cr into tScript
put " // Get various height measurements" & cr after tScript
put " var scrollHeight = Math.max(" & cr after tScript
put " document.body.scrollHeight,
document.documentElement.scrollHeight," & cr after tScript
put " document.body.offsetHeight,
document.documentElement.offsetHeight," & cr after tScript
put " document.body.clientHeight,
document.documentElement.clientHeight" & cr after tScript
put " );" & cr after tScript
put " return scrollHeight;" & cr after tScript
put "}" & cr after tScript
-- Calculate the height and return it to LiveCode
put "var height = getScrollHeight();" & cr after tScript
put "if (typeof liveCode !== 'undefined' && liveCode.ReturnPageHeight)
{" & cr after tScript
put " liveCode.ReturnPageHeight(height);" & cr after tScript
put "} else {" & cr after tScript
put " alert('liveCode object is not available');" & cr after tScript
put "}" & cr after tScript
-- Execute the JavaScript code in the specified browser widget
try
if pBrowser = empty then
do (tScript) in widget "JS Engine" of stack "AppStarterStack"
else
do (tScript) in widget ("Instance " & pBrowser) of group
"Browsers" of this card of stack "AppStarterStack"
end if
catch tError
put "JavaScript Error: " & tError
end try
end getPageHeight
On Sat, Jun 28, 2025 at 11:16 AM Andreas Bergendal via use-livecode <
use-livecode at lists.runrev.com> wrote:
> Certainly!
>
> 1) put ”liveCode.MyCommand(T.length);" at the end of your JavaScript code.
> 2) Add ”MyCommand” to the browser widget’s javascriptHandlers (either by
> script or in the field for it in the inspector)
> 3) Add the handler ”on MyCommand pT” to the browser’s script (or your card
> script or stack script).
> 4) Code whatever the handler should do with the value in pT.
>
> ”MyCommand” can of course be any name you want, as long as you are
> consequent in adding it to the browser widget’s javascriptHandlers and to a
> script that can catch it when the javascript sends it back to LiveCode.
>
> WARNING: Do NOT use .length if the value of T is a number. LiveCode then
> instantly crashes with no chance to save your changes.
> The JavaScript would return ”undefined”, and apparently neither LC9 nor
> LC10 can handle that at all. I should file a bug report on that.
>
> /Andreas
>
>
> > 28 juni 2025 kl. 16:07 skrev jbv via use-livecode <
> use-livecode at lists.runrev.com>:
> >
> > Hi list,
> >
> > When using a browser widget, I can get the whole code of a
> > page displayed in the browser with :
> > put the htmlText of widget "Browser" into myHtml
> >
> > I can also run chunks of javascript inside the browser,
> > even if the code isn't there, for instance :
> > do "var T=document.getElementsByClassName('xyz');alert(T.length);" in
> widget "Browser"
> >
> > But can I get some value generated via javascript back
> > into the LC script and use it as a variable ?
> > For instance in the above example, can I get the T.length
> > value in a variable inside the LC script, after the chunk
> > of js has ran ?
> >
> > Thank you in advance.
> > jbv
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
More information about the use-livecode
mailing list