Working with Browser objects (note: LiveCode 8.1 has a Browser widget that is much easier to work with) There are a number of ways to access the Internet from LiveCode programs. The easiest is using the "Launch" command. It runs (launches) the default application on your computer (e.g. I.E., Safari, etc) then transferring control to it. We did this in the last lesson. The better way (but more difficult one) is by creating your own browser window inside of LiveCode. You stay inside of your Livecode application and have full control over what the user does. If you want to keep everything inside your app (without going to another program), you can use a browser window (revBrowser object). This works well in either a desktop or a mobile computer. The advantage of this is that you stay within your own program or app. You open a "browser" window on your card, and go to the web address. When done, you have to close that "browser" window. It is tricky only because you have to write code to prevent errors and be very careful how and where you issue the commands. (You get errors if you do not do things in the right sequence or try to open 2 browsers with the same "ID".) First you need to initialize the revBrowser. It only has to be done once. (Just as you open up a normal browser - IE, FireFox, Safari, Chrome- and just use that one, to visit different sites). Most of the time, we put this in the openCard handler that runs when you first open the card. A. Initializing the Browser Object (Window) First create an image area for the browser window using the "image Area Tool" and size it on your card. Name it "bwin"
B. Showing Websites Now add 2 buttons: a button named "Google" and one named "msn", then add the following code to the button scripts: B-1. Allowing the user to type in an URL (website)
C. Showing PDF's, text files, and more Add a button for a PDF (e.g. the bell schedule for the day) C-1. Selecting the PDF to Show from a directory -----------Put this code on the Button Script -------------------------------- global myBrowser -------------------------------------------------------------------------------------- notes: on a PC, it might be: revBrowserSet myBrowser , "url", "C:/Users/John-Smith/My Documents/Bell Schedule.pdf" or revBrowserSet myBrowser , "url", "C:/Users/John-Smith/My%20Documents/Bell Schedule.pdf" on a Mac, it might be: It is safer to use LiveCode to determine the proper location of your "Documents" folder with: C-2. Selecting the PDF to Show from a directory -----------Put this code on the Button Script -------------------------------- global myBrowser on mouseUp local tFile -------------------------------------------------------------------------------------- note: You can restrict the choices to many other file types: JPEG ask file "Export picture as:" with type "GIF File|gif|GIFf" JPEG and Gif ask file "Export picture as:" with type "JPEG File|jpg|JPEG" or type "GIF File|gif|GIFf" Others with type "Comma Separated Values|CSV" C-3. Selecting any file from a directory -----------Put this code on the Button Script -------------------------------- global myBrowser on mouseUp local tFile -------------------------------------------------------------------------------------- D. Showing Images and Pictures(jpg, bmp, gif, ...) Add a button for a Photo, and have an image "Win" set to the size to fit on your screen D. Selecting the Image to Show from a directory -----------Put this code on the Button Script -------------------------------- on mouseUp -------------------------------------------------------------------------------------- notes: E. Showing a Map with your Location on it Go to http://maps.google.com and do a search on your school. Google maps will then show you a map with your location on it. Create a button called "Loc". Put the code below on a button on your card. This will open the map showing your school and with directions to it -----------Put this code on the Button Script -------------------------------- global myBrowser end mouseUp -------------------------------------------------------------------------------------- F. Other Changing the size of the browser: revBrowserSet myBrowser , "rect", "0,0,200,400" Making the browser window invisible: revBrowserSet myBrowser , "visible", "false" Look up revBrowserSet in the Dictionary for other options Notes:
2. Add code for the mobile and desktop platforms global myBrowser if the environment is "mobile" then if myBrowser is empty then note: if you are using version 6.7 or higher of LiveCode
on closeCard For more examples of what you can do with revBrowser there is a Browser Sampler stack in the Resources/Examples folder of your Rev distribution. You might also find the Explore LiveCode: The Internet tutorial interesting 3. If you are developing a program that is running on a mobile device, then have a look at this lesson: http://lessons.runrev.com/s/lessons/m/4069/l/22836 old: revBrowser is only supported on desktop platforms. You can view PDF files from the Internet on iOS using Browser Control. Check out this lesson on how to do it: http://lessons.runrev.com/s/lessons/m/4069/l/22836-How-do-I-use-the-Browser-Control- Another Way with more detail and checks: Here, you may just want to have separate buttons to open and close a browser window and others to go to specific websites: There are a set of commands to create and work within a browser window in LiveCode Lets build a desktop browser: 1. Open a new Main Stack Add the following handlers to your card or stack:
After you open a browser window, to open a website use the following code: put revBrowserOpen(the windowId of this stack, "http://www.msn.com") into xBrowser Dont forget to close it when you are finished 2 Single Use Websites on openMSN put revBrowserOpen(the windowId of this stack, "http://www.msn.com") into xBrowser revBrowserSet xBrowser, "visible", true revBrowserSet xBrowser, "rect", the rect of graphic "template" --revBrowserClose xBrowser end openMSN |
Home >


