Communicating with the Browser


Flash can communicate with the Web browser. The main use of this is to redirect the browser to some other URL. This can be done in the current browser window or in a new one. The function to perform these actions is called getURL , and it has the following general form:

 void getURL("  url  ", [  window  , [  variables  ]]); 

The first argument, "url" , refers to the URL that you want the browser to load. This is the same as the previous loading functions, which took a URL argument.

The second argument is called window , and it dictates where the browser should load the URL. Table 12.1 lists all possible special values for window along with a description of each. You can specify any ID here; if a window or frame exists with that name , it will load the content there.

 
Table 12.1: getURL window Values

window

Description

"_self"

Loads the URL into the current frame in the current browser window.

"_blank"

Loads the URL into a new browser window. (A new browser instance is opened.)

"_parent"

Loads the URL into the parent of the current frame in the current browser window.

"_top"

Loads the URL into the top-level frame in the current browser window.

You might notice that Table 12.1 mentions frames several times in the description. Frames are a way that an HTML document is broken into zones. Most HTML sites use frames to keep a menu visible while the user scrolls through content in another frame. A full explanation of frames is beyond the scope of this book. As a result, I'm only going to show you the " _blank" and " _self" window values.

To open a URL in a new browser window, leave the old browser window showing your Flash content and use "_blank" as the second argument to getURL , as in the following example:

 var myURL = "veggie.swf"; getURL(myURL, "_blank"); 

In this case, the URL we're opening is a local one: veggie.swf. The getURL call opens a new browser window and opens the veggie.swf file in it, as shown in Figure 12.7.

click to expand
Figure 12.7: The _blank window argument opens a new browser window for the veggie.swf file.

The URL in this case was veggie.swf, but it didn't have to be. It could have been a fully qualified URL, such as http://www.macromedia.com, which would retrieve whatever default HTML file resides at that address and put it in a new browser window.

If, on the other hand, you want to use the current browser to open a URL such that your Flash content is replaced by the new URL, you can either use the _self value or omit the value altogether, as in the following example:

 var myURL = "veggie.swf"; getURL(myURL); 

This version will replace your Flash content with the content that is located at the URL you have specified.

If you are using Flash to develop a Web page that uses frames, you should investigate all the options for window . However, because this is a games book, we're going to stop here.

Caution  

If you choose to open a new browser window, be careful that your current Flash is not running complex animations or recurring scripts. If you hog the CPU with your Flash and then open a new browser window with getURL that is CPU intensive , the user's computer might react slowly, which will naturally upset him.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

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