Page 1 of 1
Any success with iPhoneControlDo "execute" for scripts?
Posted: Tue Sep 27, 2011 8:37 am
by strongbow
Has anyone successfully used
iphoneControlDo sBrowserId, "execute", pScript
to run some javascript in an embedded browser control (UIWebView)?
If so, then how? i.e. what format should the script be in?
Are there examples somewhere?
Thanks for any pointers!
cheers
Alan
Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Sun Feb 19, 2012 12:15 am
by rca
I have had some success, but some challenges.. (assumes that the browser gMobBrowserID has already been created like
iphoneControlCreate "browser"
put the result into gMobBrowserID
This works on iOs:
put "alert('This is a message from the script');" into theFirstScript
iphoneControlDo gMobBrowserID, "execute", theFirstScript
also can do more than one line of JavaScript
put "alert('This is a message from the script');alert('This is another message from the script');" into theFirstScript
iphoneControlDo gMobBrowserID, "execute", theFirstScript
so I can execute multiple lines - even place the lines in a field for easier editing and visibility. (i.e. then "put field scriptData into theFirstScript")
BUT I can't get other simple things to work
alert(document.title);
will fail, with no error message in "the result"
In fact, if I have a field like
alert("This is a debugging message2");
alert("This is a debugging message3");
alert(document.title);
I don't get the first 2 alerts either - the whole statement fails
I find that these things (e.g. alert(document.title);) DO work on my mac, using get revBrowserExecuteScript(tBrowserId, theScript)
very frustrating....
I have tried using backslashes - doesn't work
For the lines above that do work, it seems to work with single quotes or double quotes on ios. They can have returns separating lines, in a field, or not...
also
according to Apple, "..JavaScript execution time is limited to 10 seconds .."
I need this to work for a project, so I will continue looking into it
I will try to post a test stack later today
r
Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Sun Feb 19, 2012 10:07 am
by strongbow
Hi there
I did have success with this, but I haven't been using stuff like "document.title".. What I did have success with was calling Javascript functions that are in my web page from LiveCode. So basically the same as you but it has (so far) been enough for me.
For returning values to my app I attach them to the end of my URL from inside the Javascript function using
window.location = "#?returnVal="&myReturnVal
and then catch this in browserLoadRequest and handle it if necessary.
Hope this helps.

Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Sun Feb 19, 2012 5:31 pm
by rca
Hi Strongbow
Thanks for the feedback. Based on this forum, the live code Developer mailing list and support feedback, you and I are among the four people in the world trying to do this stuff.

Surprising, as I think the internet is going to be BIG

, and Apple too..
I did post to another part of the forum, re "..you cannot exchange data in real time with JS in IOS browser.."
http://forums.runrev.com/viewtopic.php? ... 091#p52091
Calling a Javascript in the page is a good idea, and will work in some cases where I have control/input to the page, but for what I am trying to do that is not always a solution.
I will keep digging
btw I am Australian, living in California - good to see New Zealand, Australia active in these things
regards
Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Sun Feb 19, 2012 7:07 pm
by strongbow
G'day there
Yup, as I have control over my page I can add functions etc as required. Also enables me to post stuff back to my LiveCode app that can then process it in "real-time"...

But, as you say, you have some other requirements. Haven't tried what you say so will be interested to see what you can come up with... will also let you know if I manage anything interesting.
Good luck in Cali - I'm a Kiwi in Germany... Drop in if you're over this way!

Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Tue Apr 24, 2012 10:15 pm
by BarrySumpter
Hi strongbow,
Any way I can get you to post a sample app on how you are doing this?
i.e. posting back to LC app for real time processing?
tia
Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Tue Apr 24, 2012 10:47 pm
by strongbow
Hi Barry
Well, the above bit explains one part of it:
For returning values to my app I attach them to the end of my URL from inside the Javascript function using
Code: Select all
window.location = "#?returnVal="&myReturnVal
and then catch this in browserLoadRequest and handle it if necessary.
The actual URL that you load could be on a website or even a local html file with included javascript functions.
To call a JS function from LC I just load a javascript function call into a variable and then call it using
Code: Select all
iphoneControlDo pBrowserId, "execute", pScript
where pBrowserId is the id of your iOS browser instance and pScript is the JS function, eg. computeDistance()
Inside that function, I would have the line:
Code: Select all
window.location = "#?returnVal="&myReturnVal
or something similar - this adds that string to the URL to be loaded. And then you catch that inside LC by handling:
Code: Select all
on browserLoadRequest pUrl, pReason
Here you would check if pURL contained the bits you added (i.e. returnVal=....) and if so, handle it inside LC.
I know this isn't that clear, probably a diagram of the process would be most understandable, but hope that it helps in some way.
cheers
Alan

Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Tue Apr 24, 2012 11:16 pm
by BarrySumpter
Hi Alan,
Many thanks for the reply.
Thanks for clarifying the logic behind this concept.
Where a picture is worth a thousand words,
a working sample app is worth a billion.
I'll know what to do when I get to the iOS version of my app.
Or when RR opens up the browser events for Android.
Thanks again.
Re: Any success with iPhoneControlDo "execute" for scripts?
Posted: Wed Jul 24, 2013 12:50 am
by colourpixels
just thought I'd post that I found this thread very useful, using the technique to trigger animations created using Hype (HTML 5 editor). So thanks for the post.