Communicating Locally

     

Your Macromedia Flash movies can run in one of several environments. The most common environment is the Flash Player in a Web browser. You can also run a Flash movie as a standalone projector or inside a Macromedia Director movie. In all these cases, the movie can communicate with the environment in a number of ways. In this section, you'll look at how the movie can communicate with the local environment if it is running in the Flash Player in a browser or as a projector.

Two important means of communicating locally, Shared Object and LocalConnection, are covered in Chapter 21, "Advanced ActionScript," under "The Shared Object Class," page 602 , and "The LocalConnection Class," page 596 .


You can embed Flash MX movies into Macromedia Director MX as cast members . This is one of the most powerful aspects of both Flash and Director. To find out more about using Macromedia Director, see Special Edition Using Macromedia Director MX by Gary Rosenzweig (ISBN: 0789729032).


Controlling the Browser

Flash movies can work just like the HTML <A HREF> tag when viewed in a Web browser, acting as a navigation control for the browser and changing the page being shown in the browser. They can also affect the page being shown in other browser windows and frames .

Using getURL()

The main tool for controlling the browser is the getURL() global function. This global function sends a message to the browser to tell it to load a new page from a specified URL. Calling the getURL() global function is just like a user clicking a link in an < A HREF> tag in the browser.

The following button script uses the getURL() global function to load a page named newpage.html:

 on (release) {     getURL("newpage.html"); } 

NOTE

The Flash Player does not perform the actual page loading. It sends a message to the browser. It is then up to the browser to do the work.


A URL is a Uniform Resource Locator. It is the one required parameter of the getURL() global function.

There are two types of URLs: relative URLs and absolute URLs. A relative URL can be a local file, such as the newpage.html file in the previous example. It can also be a relative path to a file or directory.

An example of a relative URL that specifies a file in a directory below the current one is myfolder/newpage.html . This example specifies the file newpage.html, which is found in the folder named myfolder.

An absolute URL is a complete path to a file or directory on a server. An example of an absolute URL is http://www.mysite.com/newpage.html .

You can use getURL() with a relative URL to load a page on the same server as the current Web page. You can also use a relative URL to load a page on your local hard drive when the page is on the hard drive or when the movie is running as a projector.

You can use an absolute URL in getURL() only if you have an Internet connection because it usually calls out to a site on a Web server, not a page on your local machine.

Because the power of getURL() is not in Flash, but rather in the Web browser, you can use anything as a parameter that the browser understands as an address. For instance, if you want to make the browser list the files on an FTP server, you can use a URL such as ftp://ftp.myserver.com/ . The browser lists the files there, as long as the browser supports FTP and the FTP site supports anonymous FTP, presuming no username or password is required.

Targeting Windows and Frames

A second parameter that you can give getURL() is a target. This target can be a browser window, a browser frame, or a special command.

All Web browsers can have multiple windows open , each showing a different URL. These windows can be different sizes and have different toolbars , depending on how they were opened and how the user has adjusted them. You can also have multiple URLs visible in the same browser window by dividing it into frames. This is done at the HTML level with special HTML tags. Each frame in the window holds a separate URL and can be altered without affecting the other frames. Most browsers can also accept special commands in their address fields as alternatives to URLs. These commands change the browser's behavior or get information about the browser.

To give getURL() a window or frame name, you first have to name your browser frames and windows. Naming them takes some planning when you create your HTML pages.

Suppose you have a two-frame page with a small navigation bar on the left and a large content frame on the right. The content frame can be named contentFrame. The navigation frame can have a Flash movie in it with navigation buttons. One of these buttons can direct the contentFrame to go to a new page like this:

 on (release) {   getURL("newpage.html","contentFrame"); } 

Targeting a frame is as simple as that. The same command would even allow one browser window with a Flash movie to control the URL shown in another window. You could have a movie in the main browser window that controls a smaller pop-up window.

Creating New Browser Windows

If you use getURL() with a target that doesn't exist, the browser creates a new window to hold the content. For instance, if you target newWindow, and no window has that name, a new window named newWindow opens.

When this window exists, you can continue to address it by this name. So you can use newWindow as a target over and over again. The first time you use newWindow, the browser creates the window. Each time after that, you just change the content in the window. So a new window can be created, and you can also change its content later by using the same name. You can show HTML content to the user by creating and reusing the same window.

Using Special Targets

In addition to using your own names for targets, you can also use one of four special targets. Their names all begin with an underscore character. Here is a list of these special targets:

  • _self ” This target is the same as using no target parameter at all. It targets the current window or frame.

  • _parent ” This target refers to the frameset one level up from the current page.

  • _top ” This target refers to the window where the current frame or page is located. The page replaces all the frames in the current window.

  • _blank ” This target creates a new window. Using this target is the only way to guarantee that a new window will open, regardless of what any current windows are named.

To open a new window with a button, you could use a script such as this:

 on (release) {     getURL("newpage.html", "_blank"); } 

Calling JavaScript Functions

Although getURL() allows you to load new pages with your Flash movie, you can take almost complete control of the browser by using the fscommand() global function. Using this global function, you can send messages to JavaScript in the browser. In addition, your Flash movie can receive messages from JavaScript on the Web page as well.

Problems with JavaScript

Before you consider using Flash-to-JavaScript communication, you should know about JavaScript's shortcomings. Mainly, it doesn't work in several browser variations.

To communicate between Flash and JavaScript, you need to have a piece of software that facilitates this communication. For Internet Explorer on Windows, this piece of software is called ActiveX. For older versions of Netscape Communicator, this piece of software is called LiveConnect. Both of these pieces of software are built-in parts of their respective browsers.

Although ActiveX is a part of Internet Explorer in Windows, it is not a part of Internet Explorer for the Mac. LiveConnect is a part of Netscape browser versions 4.7 and earlier, but it is not a part of Netscape 6 on either Mac or Windows. This leaves quite a few browser/platform variations where the fscommand() will not work: Windows users with Netscape and most Mac users. You can assume that any other browser, such as iCab or Opera, also does not support this communication. For this reason alone, most developers avoid Flash-to-JavaScript communication.

TIP

If you decide to use fscommand() , put a warning on your site for Mac and Netscape users so they understand the movie will not work completely with their browsers.


For more on calling JavaScript functions, see javascript_ch22.htm on the CD accompanying this book.


Alternative Techniques

Another way to communicate from Flash to the browser relies on the fact that most browsers accept JavaScript commands in the Address field at the top of the window. You can use this technique to get around the Mac and Netscape limitations for standard JavaScript communication as mentioned earlier in the chapter.

For instance, you can open your browser and type javascript: window.alert('Hello!'); . When you press Return (Mac) or Enter (Windows), the JavaScript after the colon should run, displaying an alert box.

When Flash executes a getURL() function, it is really just sending a string to the Address field in the browser. So, by using javascript: plus some JavaScript, you should be able to trigger the browser to do just about anything.

You can issue JavaScript commands or even call JavaScript functions that have been defined on the page. This technique works in most browsers, including more recent versions of Netscape on Windows and Internet Explorer on Mac. You can quickly test any browser by using a javascript: command in the Address field.

Although this technique is a good way to send messages from Flash to the browser, it doesn't help you send messages from the browser to Flash. It also has other drawbacks: For example, it frequently replaces the contents of the current browser window with the results of the JavaScript function you called.

Controlling the Projector

fscommand() has a completely different functionality if the Flash movie is running inside a projector. In that case, you're sending messages to the projector player, which accepts a limited, but useful, set of commands. For instance, you can force the projector to resize itself or quit when the user presses a button.

Modifying the Window

When using fscommand() in a projector, you still give it two parameters: command and arguments . The commands, however, are hard-coded into the projector, so you need to choose from a small set.

The fullscreen command enlarges the projector to the full size of the user's monitor. It stretches the contents of the Flash movie to fit this new window size . The result is that the movie takes over the whole screen. There aren't even any window borders or a title bar left.

To enlarge the projector to full screen, use this command:

 fscommand("fullscreen", true); 

To return the projector to its original size, use this one:

 fscommand("fullscreen", false); 

You can also adjust whether the movie inside the window scales . To turn off scaling, use the following:

 fscommand("allowscale", false); 

Then, when the projector is full screen, the movie remains its original size, centered in the window. This is also true when the user clicks and drags the window corner to enlarge or shrink the window. You can turn scaling back on by using the following command:

 fscommand("allowscale", true); 

Hiding the Context Menu

In a projector, a user can Cmd-click (Mac) or right-click (Windows) to bring up a context menu. This menu allows the user to pause the movie or change the volume. You can disable this feature in a projector by using this command:

 fscommand("showmenu", false); 

You should disable this menu if you don't want the user controlling different aspects of your movie. You can turn it back on by using this command:

 fscommand("showmenu", true); 

Note that turning off this feature does not turn off the context menu completely. The user can still access Settings and About categories, but he cannot control the movie.

If you want to turn off these context menus when the movie is playing in the browser, you can turn off the Display Menu option in the HTML Publish Settings. This inserts a menu parameter with a value of false in the OBJECT and EMBED tags. Or you can insert these tags in the HTML yourself.

Other Commands

The quit command simply exits a projector. Although it doesn't need a second parameter, fscommand() insists on a second parameter, so you can give it anything:

 fscommand("quit", ""); 

You can also launch external applications with fscommand() . All you need to do is feed the command " exec " , plus the path to the application:

 fscommand("exec", "test.bat"); 

Because of the exec command's potential for misuse, Macromedia has limited it to running from a subdirectory named fscommand . You must create a subdirectory named fscommand under the directory where your projector will run. Put the .exe , .bat , or other files that you wish to execute in the fscommand subdirectory. Do not put the projector itself in a subdirectory. As shown in the preceding example, you do not include the subdirectory name in the exec call.



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net