Home‎ > ‎

Using Browser Object to Show Websites,PDFs,Docs,Pictures,etc

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.


1. 
Accessing the Internet (Files, Docs, etc) - Internally in LiveCode (in a browser window)
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"
                                 


    -----------Put this code on the Card Script --------------------------------

        global myBrowser

        on browserOpen
                if myBrowser is empty then
                     put revBrowserOpenCef(the windowid of this stack,"") into myBrowser
                end if
                revBrowserSet myBrowser"showborder","true"
                revBrowserSet myBrowser"visible","true"
                revBrowserSet myBrowser"rect",rect of image "bwin"
        end browserOpen

        on browserClose
                revBrowserClose myBrowser
                put empty into myBrowser
        end browserClose
  --------------------------------------------------------------------------------------

note: there are 2 different browser commands that you can use:

        1. revBrowserOpenCef - This one uses the Chrome browser webkit and opens a Chrome based browser
       2.  revBrowserOpen - This one uses the IE browser webkit (for PCs) or Safari webkit (for Mac computers)

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)

  -----------Put this code on the "Google" Button --------------------------------

             global myBrowser

               on mouseUp
                    browserOpen
                    revBrowserSet myBrowser , "url", "Google.com"
               end mouseUp

  -----------Put this code on the "Msn" Button --------------------------------

             global myBrowser

               on mouseUp
                    browserOpen
                    revBrowserSet myBrowser , "url", "msn.com"
               end mouseUp

    --------------------------------------------------------------------------------------
 
                         
      


       ------------------------------------------------------ --------------------------------


    B-2. Allowing the user to type in an URL (website)

    Add a text input field called "newurl" and a button called "Go To"
    
    -----------Put this code on the "Go To" Button Script --------------------------------

             global myBrowser
             local x

               on mouseUp
                    browserOpen

                    put field "newurl" into x
                    revBrowserSet myBrowser , "url", x
               end mouseUp

            global myBrowser

         --------------------------------------------------------------------------------------


 C. Showing PDF's, text files, and more

              Add a button for a PDF (e.g. the bell schedule for the day)
                 Put the code below on a button on your card. This will open the pdf in the "browser image" field that you created above

             C-1. Selecting the PDF to Show from a directory

               -----------Put this code on the Button Script --------------------------------

                       global myBrowser

                       on mouseUp
                                browserOpen
                                revBrowserSet myBrowser , "url", "/User/Admin/Documents/Bell Schedule.pdf"
                       end mouseUp

                --------------------------------------------------------------------------------------


                    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:
                                   revBrowserSet myBrowser , "url", "/User/Admin/Documents/Bell Schedule.pdf"

                            It is safer to use LiveCode to determine the proper location of your "Documents" folder with:
                                   revBrowserSet myBrowser , "url", specialFolderPath(Documents) & "/Bell Schedule.pdf"



            C-2. Selecting the PDF to Show from a directory

                -----------Put this code on the Button Script --------------------------------

                    global myBrowser

                   on mouseUp
                             browserOpen

                             local tFile
                             answer file "Please choose the file you would like to display" with type "PDF document|pdf|PDF"
                             if it is not empty then
                                    put it into tFile
                                    revBrowserSet myBrowser , "url"tFile
                            end if
                    end mouseUp

                 --------------------------------------------------------------------------------------

                note: You can restrict the choices to many other file types:

                           JPEG
                                    ask file "Export picture as:" with type "JPEG File|jpg|JPEG" 

                            GIF

                                 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"
                                 with type "text files|TXT"
                                 with type "Applications|exe|*"
                                 with type "Applications|app|APPL" 


           C-3. Selecting any file from a directory

                -----------Put this code on the Button Script --------------------------------

                    global myBrowser

                    on mouseUp
                            browserOpen

                            local tFile
                            answer file "Please choose the file you would like to display" 
                            if it is not empty then
                                    put it into tFile
                                    revBrowserSet myBrowser , "url"tFile
                            end if
                    end mouseUp

                 --------------------------------------------------------------------------------------

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
                 Put the code below on a button on your card. This will open the pdf in the "browser image" field that you created above

             D. Selecting the Image to Show from a directory

               -----------Put this code on the Button Script --------------------------------

                        on mouseUp
                                if exists (img "picture") then
                                        delete img "picture"
                                end if
                                answer file "Choose the picture to view:"
                                if the result = "cancel" then
                                      exit mouseUp
                                else
                                      import paint from file it
                                      set the name of the last image to "picture"
                                end if
                                set the rect of image "picture" to the rect of image "win"

                --------------------------------------------------------------------------------------


                    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.
                Copy the url at the top of your map window and use it in your app

                 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

                       on mouseUp
                             
  browserOpen   
                               revBrowserSet myBrowser , "url", "https://www.google.com/maps/place/Eleanor+Roosevelt+High+School/@38.994039,-76.86945,17z/data=!3m1!4b1!4m2!3m1!1s0x89b7c3d4f1ac3cb5:0x50fd0de25231d44"

                        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: 

1. if a filename has a space in the document name then revBrowser will silently fail. Replacing space with %20 cures this.

    replace " " with "%20" in myUrl

2. You can do a lot more with revBrowser that we have discussed in this lesson, for more information have a look at the revBrowser functions and commands in the Dictionary. 



2. Add code for the mobile and desktop platforms

        global myBrowser

         if the environment is "mobile" then
                if myBrowser is empty then
                        mobileControlCreate 
"browser"
                        put the result into myBrowser
                end if
                mobileControlSet myBrowser"rect"the rect of group "Browser"
                mobileControlSet myBrowser"visible""true"
                mobileControlSet myBrowser"url""http://www.google.com"
        else

               if myBrowser is empty then
                       put revBrowserOpen(the windowid of this stack,"www.google.com") into myBrowser
                end if
  
              revBrowserSet myBrowser"showborder","true"
                revBrowserSet myBrowser"visible","true"
                revBrowserSet myBrowser"rect",rect of image "bwin2"
        end if


                note: if you are using version 6.7 or higher of LiveCode
                         Replace the line (above)
                                    put revBrowserOpen(the windowid of this stack,"www.google.com") into myBrowser
                         With
                                    put revBrowserOpenCef(the windowid of this stack,"www.google.com") into myBrowser


                 It has controls and works so much better


        on closeCard
                if the environment is "mobile" then
                        mobileControlDelete myBrowser
                else
                       revBrowserClose myBrowser
                      put empty into myBrowser
                end 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:

global xBrowser

on openBrowser   
   if xBrowser is not empty then
      answer "This app cannot create more than on browser instance. Please close this before creating another."
      exit openBrowser
   else
      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"
      if xBrowser is not an integer then
         answer "Failed to open browser"
      end if      
   end if   
end openBrowser

on closeBrowser
   revBrowserClose xBrowser
   put empty into xBrowser
end closeBrowser

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     
Comments