Popup Windows With Java in Flash
Popup Windows With Java in Flash
This tutorial describes a method of using JavaScript within your Flash movie without using any external JavaScript in the html your movie is embedded in. For the example we are going to be creating pop-up windows. This tutorial starts off with the basic method involved then goes into some more advanced effects. And finally finishes off by describing how to dynamically update the movie located in your pop-up with JavaScript from your parent movie.
Overview
This becomes a powerful method because it enables you to include JavaScript in any Flash movie regardless of where it is located. For example with this tutorial you can open up a new window (with specific dimensions, location, and browser features) from a banner ad or from your Flash footer for various message boards, regardless of what JavaScript is located in the document in which it is embedded. Traditionally you would have to include predefined JavaScript in the html document that the movie is embedded in. After we go over the basic technique we will be going into various effects using JavaScript such as shaking the pop-Up window, moving the pop-up once loaded, opening and closing the pop-up at specific delay times, scrolling to a specific location in your pop-up, etc etc. Then goes on to explain how to control that pop-up from the parent movie timeline - using this you can for example shake a pop-up window you previously opened at a specific time or event in your Flash movie. Some features will not be available to all browsers as well as when you are opening a pop-up window not located on the same server as the parent movie. The example for this tutorial provides you with a JavaScript code generator that you can use to create all the effects we will be covering plus more. Every single JavaScript property, method, and function related to pop-up windows was attempted to be included for use in the Auto-Code Generator - for this reason some will not be available to all browsers (most will though). You can use/review the auto-code generator at this link http://www.flash-db.com/PopUp In the end of this document I provided a brief overview of how to use the Auto-Code Generator, but it should be fairly self explanatory.
width: A numeric value representing the width of the popup window height: A numeric value representing the height of the popup window
left: A numeric value representing the left (X) position of the popup window top: A numeric value representing the top (Y) position of the popup window toolbar: A Yes / No value indicating whether to add a toolbar to the popup window location: A Yes / No value indicating whether to add the URL Location to the popup window scrollbars: A Yes / No / Auto value indicating whether or not to include scrollbars in the window status: A Yes / No value indicating whether to add the bottom Status/Loading part to your window resizeable: A Yes / No value indicating whether to allow the window to be resized once opened fullscreen: A Yes / No value indicating whether or not to open the window fullscreen. This will open a borderless full screen window in IE but will not work with Netscape.
Then we add the NewWindow.focus(); part, this ensures that the popup will be opened on top of all other windows. You can change this to NewWindow.blur(); (which is the opposite of focus except it pushes the window behind any other browser windows that are open. Then we close off the code by adding the void(0); portion. As you can see that since we are able to add additional functions such as NewWindow.focus(); we will be able to add quite a few more functions and effects for this pop up window. The next part describes some of the more useful ones. Also their is no real need to place this script on a button - you can just as easily place it on a certain frame in your flash movie at a specific point you want your window to open. An example would be: getURL ("javascript:NewWindow=window.open('ShowPopup.php','newWin','width=400,height=300,left=0,top=0, toolbar=No,location=No,scrollbars=No,status=No,resizable=No,fullscreen=No'); NewWindow.focus(); void(0);"); Just don't add the on (release) { portion. With this technique you can continue to open windows place at specific locations as your movie continues to play.
Display an Alert before window is opened then scroll to a specific point 3 seconds after it loads
on (release) { getURL ("javascript:NewWindow=window.open('ShowPopup.php','newWin','width=40 0,height=300,left=0,top=0, toolbar=No,location=No,scrollbars=No,status=No,resizable=No,fullscree n=No'); alert('Flash Rules'); NewWindow.focus(); setTimeout('NewWindow.scroll(0,400)',3000); void(0);"); }
The above code will open a new window with the various properties as indicated. Then display an Alert box that contains the text 'Flash Rules' before the window opens. The alert('Flash Rules'); is the code that will activate the alert. Then we add the code setTimeout('NewWindow.scroll(0,400)',3000); to Scroll the window down by 400 px the (0,400) part represents the X and Y coordinates you want to scroll by. The setTimeout part adds a delay to the scroll, in this case the delay time is 3000 milliseconds or 3 seconds, before the scroll happens. It can be important at times to add a delay because you can't scroll a page that has not loaded.
on (release) { getURL ("javascript:NewWindow=window.open('ShowPopup.php','newWin','width=40 0,height=300,left=0,top=0, toolbar=No,location=No,scrollbars=No,status=No,resizable=No,fullscree n=No'); NewWindow.focus(); setTimeout('NewWindow.resizeTo(500,500)',1000); setTimeout('NewWindow.close()',6000); void(0);"); }
The above code opens a popup window, the resizes it after 1 second to the new dimensions of (500,500) instead of 400 x 300. This is done by the code: setTimeout('NewWindow.resizeTo(500,500)',1000);. Then it is closed after 6 seconds by the code setTimeout('NewWindow.close()',6000);. The most important thing to notice about the delay close code is the NewWindow.close() This is the basic code that will close a popup window. As you can see by now their is really no limit to the amount of effects that you can add. The main limitation is not with the JavaScript or Flash but with Browser compatibility. The resizeTo and scroll functions will not work with all browsers. You can get fairly creative by mixing and matching functions and portions of the code. The next section deals with manipulating and adding more effects to the Popup window you recently opened from the parent Flash movie.
Manipulating, Moving, Resizing, Scrolling, and Animated effects for the window Once opened
The following reference provides ways to control you popup window once opened. You can either place the code on buttons that control the popup or on frame actions that will manipulate the popup window at certain time's when the movie is playing. We'll start off with some simple functions then move onto some more complex ones. You can also combine these with the previous section and have the various affects occur when the popup is loaded.
on (release, rollOver) { getURL ("javascript:NewWindow.focus(); for (i=10; i>0; i--) { for (j=6; j>0; j--) { NewWindow.moveBy(0,i); NewWindow.moveBy(i,0); NewWindow.moveBy(0,-i); NewWindow.moveBy(-i,0); }} void(0);"); }
Probably the best effect. This effect shakes the popup window for specific intervals and shakeness (couldn't think of better word). The first thing that we do is bring the popup window to the front of the browser, like always withNewWindow.focus(); Then we use two 'for' loops to move the window back and forth and up and down really fast. You can change the settings by changing the values for i and j.
on (release, rollOver) { getURL ("javascript:NewWindow.focus(); for (j=150; j>0; j--) { NewWindow.moveBy(2,1.33333333333333); } void(0);"); }
The above function shows how to create an animated Moving window. This will move your popup window across your screen. Like always we use the focus(); command to move the window to the front before we move it. Then we use a 'for' loop to move the window across the screen in small increments. In this case the variable jindicates how many times it will incrementally move. By increasing j to say j=300 will increase the smoothness of the move creating smaller increments. Watch out though if you make j to large you'll freeze up your computer and get stuck in an infinite loop. This is basically the same code used for the Animated Resize and Animated Scroll. To change this over to Animated Scroll or Animated Resize you would for example change the NewWindow.moveBy( portion toNewWindow.scrollBy(.
Adding custom text, message's, and Links to your Popup window with JavaScript + PHP
This is where we get a little bit more technical and use a little bit of PHP - The php used in this example is so simple though that you should be able to convert it over to ASP or CFM with little or no experience in either. Please note that PHP or any other server side scripting language is not needed before this section. And can be avoided all together with the document.write function in JavaScript. Here's the basic code used when we open the popup window:
on (release) { getURL ("javascript:NewWindow=window.open('ShowPopup.php?customName=Jeff&cus tomLink=http://www.flashkit.com& customMessage=This is my Custom Message','newWin','width=400,height=300,left=0,top=0,toolbar=No,locat ion=No,scrollbars=No, status=No,resizable=No,fullscreen=No'); NewWindow.focus(); void(0);"); }
The part in red is the only part where changing from the original 'basic' code. What where doing here is attaching variables onto the end of the URL string. Then we embed the Flash movie in a php page (which is the same as html, with the exception that we can add scripting to it). The PHP file would contain the following code in any part before we embed the movie:
<? if ($customName || $customLink || $customMessage) { $Attach = "?customName=$customName&customLink=$customLink&customMessage=$custom Message"; } else { $Attach = ""; } ?>
What this does is to grab the variables indicated with a preceding $ sign from the URL - then put them into a format we can attach onto the end of the SWF embed tags. The $Attach variable is used to store all of them. The embed code for the Flash movie would then look like this:
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swf lash.cab#version=5,0,0,0" WIDTH=820 HEIGHT=640> <PARAM NAME=movie VALUE="display.swf<? print"$Attach";?>"> <PARAM NAME=quality VALUE=high>
<PARAM NAME=bgcolor VALUE=#0066CC> <EMBED src="display.swf<? print"$Attach";?>" quality=high bgcolor=#0066CC WIDTH=820 HEIGHT=640 TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P 1_Prod_Version=ShockwaveFlash"> </EMBED> </OBJECT>
The most important part of the above embed code is indicated in Red. This will just print out the variables that we earlier defined with the $Attach variable onto the end of the flash movie. These variables will automatically be declared in the movie. Theirfor all you have to do to the Flash movie in order to see your custom text is to create a dynamic text field named for example 'customName' or 'customMessage'. For the link to work all you need to do after this is to create a button and add the following code:
Conclusion
That's about it. Hope you found this interesting. Have fun playing around with Pop-ups. -Jeffrey F. Hill