Task: Open a New Window

I l @ ve RuBoard

Task: Open a New Window

Perhaps the most common task that Flash developers demand from browser communication is the ability to open a new browser window. This is something that can be done with both the getURL command and JavaScript communication.

However, JavaScript has one distinct advantage: You can specify the size and properties of the new window. Here's how to do that:

  1. Create a new Flash movie.

  2. Place a button in the movie.

  3. Attach this script to the button:

     on (release) {   fscommand ("newwindow", "content.html"); } 
  4. In the Publish settings, choose to export an HTML page as well as a Flash movie. In the HTML page, choose Flash with FSCommand as your template.

  5. Publish the movie.

  6. Open the HTML page in a text editor. Look for the portion of the JavaScript function where you can place your own commands. Insert this in that spot:

     if (command == "newwindow") {     window.open(args,"","width=320,height=240,location=no,toolbar=no, menubar=no"); } 
  7. Create a simple HTML page named content.html.

  8. If you open the HTML page in a browser capable of supporting JavaScript communication, you should be able to click on the button and get the window to appear. It should be 320240 without a toolbar.

  9. But what about browsers that don't support JavaScript communication? Well, we can use a getURL command to open a standard new window in them. However, the trick is knowing which command to execute in which situation.

    Back in the HTML page, add the following JavaScript function to the end of the JavaScript inserted by Flash's Publish command:

     function initComm() {     window.document.newWindowMovie.SetVariable("jsCommOK","OK"); } 

    This function tries to set the variable jsCommOK in the Flash movie to the string "OK" . If the browser is capable of JavaScript communication, the command should be successful. If not, jsCommOK will remain undefined.

  10. To run the initComm function at the right time, you need to edit the BODY tag of the HTML page:

     <BODY bgcolor="#FFFFFF" onLoad="initComm();"> 

    This runs the function only after the HTML page is finished loading. If you run it any earlier, the Flash movie may not be there to get the message.

  11. Now, go back to your button script. Change it to call either fscommand or getURL depending on the value of jsCommOK .

     on (release) {     if (jsCommOK == "OK") {         fscommand ("newwindow", "content.html");     }  else {         getURL ("content.html", "_blank");     } } 

The folder 17newwindow contains all the files used to make up this example. In addition, the Flash movie has a dynamic text field linked to the variable jsCommOK . You should see this variable change values as the initComm function sends its message.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

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