在AS2中因为程序不检查变量是否以定义,所以在AS2中使用FlashVars是非常轻松的一件事。
AS2:
if(test!=undefined)
{
trace(test);
} |
HTML:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>FlashVars Test</title> <script type="text/javascript" src="swfobject.js"></script> </head> <body> <div id="flashcontent"> </div> <script type="text/javascript"> // <![CDATA[ var so = new SWFObject("flashvarstest.swf", "sotester", "550", "400", "9", "#ffff33"); so.addVariable("testvar", "Hello World!"); so.write("flashcontent"); // ]]> </script> </body> </html> |
在AS2这样的代码是完全没有问题,但到了AS3中这种方法将会显示变量未定义的错误.在AS3中仍旧存在一种解决方案来实现这种功能.
AS3:
var varName:String;
var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
trace(paramObj.test); |
HTML:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>FlashVars Test</title> <script type="text/javascript" src="swfobject.js"></script> </head> <body> <div id="flashcontent"> </div> <script type="text/javascript"> // <![CDATA[ var so = new SWFObject("flashvarstest.swf", "sotester", "550", "400", "9", "#ffff33"); so.addVariable("testvar", "Hello World!"); so.write("flashcontent"); // ]]> </script> </body> </html> |