5:26 PM 11/22/2008

PopupFromFlash javascript/actionscript class

Matt Lohkamp
work@mattlohkamp.com
http://mattlohkamp.com

This method handles the process of calling a javascript popup using the window.open function from a flash movie embedded in a web page - if this fails (due to aggressive popup blocking, e.g. Safari) a normal window is opened, as if a link with target='_blank' was clicked.

Refer to 'readme.txt' for documentation, peruse 'example implementation.htm' and 'Example.fla' for a working example of the class in action, and check out the inline comments in PopupFromFlash.as and PopupFromFlash.js if you want to customize it.

The examples utilize swfObject to embed the .swf in the html page - for more information, visit http://code.google.com/p/swfobject/

GETTING STARTED:

1.) include 'PopupFromFlash.js' in the html page

HTML:	<script src="swfobject.js" type="text/javascript"></script>

2.) embed the .swf in the html, give it an 'id' attribute

HTML:	<object id="flashContent" height="480" width="640" type="application/x-shockwave-flash" data="Example.swf">

3.) in the .swf, include the PopupFromFlash class

AS3:	import PopupFromFlash;

4.) run the init function on the PopupFromFlash class

AS3:	PopupFromFlash.init();

5.) when you want a popup to be called, run the popup function on the PopupFromFlash class - pass in the website you wish to pop up, and the id attribute that you gave your .swf embed in the html

AS3:	PopupFromFlash.popup('http://website.com', 'flash embed ID');

6.) open the page in a browser - when the popup function is called in the .swf, the javascript class will attempt to create a popup using the window.open function - if that doesn't work, it will report back to the .swf, which will open a new window using the navigateToURL function instead. If that fails, an alert box will pop up, pointing out the error and providing the URL.

DOCUMENTATION:

AS3 PopupFromFlash class 

 - methods -

PopupFromFlash.init	This function should only be run once per .swf, as soon after importing the class as possible. It uses ExternalInterface to add a callback function to the html page DOM, which instructs flash to run the nagivateToURL function - this will trigger if the window.open method of popping up the window fails.

PopupFromFlash.popup	This function passes information to the popupFromFlash object in the html DOM, and will trigger a popup according to the specified paramets.
	 - arguments -
	pageURL:String	(required) the URL that will be loaded by the popup.
	swfID:String	(required) the id attribute of the .swf's object or embed tag in the html.
	options:Object	(optional) this configures the popup, and allows the following properties:
		windowTitle:String	some browsers allow the popup to display a custom title, which will be set to this value.
		useRandomSeed:*		if multiple popups are called with the same custom title, some browsers will re-use the same window. It accepts the following values:
			false:Boolean	a false value will keep the default 're-use' behavior.
			'append':String	this will append a unique number to the end of the custom page, forcing the browser to open a new window.
			true:Boolean	setting the value to true will ignore the custom window title and use the unique number instead.
		windowParams:*		window.open allows the popup to be configured in certain ways, dictated by this parameter - the Object form is mostly a matter of organizational convenience, and is equal to the String form in terms of functionality supported. It accepts the following values:
			String		if windowParams is a String, it must be formatted according to the expectations of window.open - currently those specs are available from Microsoft at http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx.
			Object		If windowParams is an Object, its properties will be formatted into the string format expected by window.open - see above note for details. As an example, {width:800,height:600} will be formatted as String('width\800,height=600').

Javascript PopupFromFlash class

 - methods -

openWindow	This function will normally only be called from the embedded .swf, and may not support the 'fall-back' behavior if window.open fails when called from the page (as opposed to the .swf)
	- arguments -
	swfID:String	(required) the id attribute of the .swf's object or embed tag in the html.
	url:String	(required) the URL that will be loaded by the popup.
	title:String	some browsers allow the popup to display a custom title, which will be set to this value.
	useRandomSeed:*	if multiple popups are called with the same custom title, some browsers will re-use the same window. It accepts the following values:
		false:Boolean	a false value will keep the default 're-use' behavior.
		'append':String	this will append a unique number to the end of the custom page, forcing the browser to open a new window.
		true:Boolean	setting the value to true will ignore the custom window title and use the unique number instead.
	params:String	window.open allows the popup to be configured in certain ways - currently those specs are available from Microsoft: http://msdn.microsoft.com/en-us/library/ms536651(VS.85).aspx.
getWindows	Returns an array of Objects chronicling the popup history of the object. Each object has the following properties:
	title:String	references the custom title of the popup, disregarding the seed
	generatedTitle:String	references the entire title of the popup, including the seed
	window:Window	this refers to the actual window object of the popup's DOM - make sure you know about the security restrictions in place concerning manipulation of other window's DOM before you try anything, and check out Microsoft's documentation: http://msdn.microsoft.com/en-us/library/ms535873(VS.85).aspx