学会用timer解决延迟加载的问题……
flash在接收js参数或者flash在加载外部文件的时候可能会有时间延时而导致在调用的时候出现错误。此时可以定义一个定时器,只要设定合适的时间就能完美解决问题。下面是我遇到的问题,我在通过js获取浏览器尺寸之后再将尺寸传递给flash的时候,页面加载swf文件是需要时间的,如果js执行完之后,swf文件没有加载完成,那么js参数就无法传递过去。flash可以定义一个timer,在一段时间之后调用js函数,然后得到返回数据(浏览器的尺寸),具体代码如下:
页面:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<link rel="shortcut icon" href="http://www.d2show.net/favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>dfuse</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style></head>
<body bgcolor="#000">
<script language="javascript">
if (AC_FL_RunContent == 0) {
alert("This page requires AC_RunActiveContent.js.");
} else {
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','100%','height','100%','id','index','align','middle','src','main','quality','high','bgcolor','#393936','name','index','allowscriptaccess','sameDomain','allowfullscreen','false','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','main' ); //end AC code
}
</script>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="index" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value="main.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000" /> <embed src="main.swf" quality="high" bgcolor="#000" width="100%" height="100%" name="index" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<script language="JavaScript">
var winWidth = 0;
var winHeight = 0;
function findDimensions() //函数:获取尺寸
{
//获取窗口宽度
if (window.innerWidth)
{
winWidth = window.innerWidth;
}
else if ((document.body) && (document.body.clientWidth))
{winWidth = document.body.clientWidth;}
//获取窗口高度
if (window.innerHeight)
{winHeight = window.innerHeight;}
else if ((document.body) && (document.body.clientHeight))
{winHeight = document.body.clientHeight;}
//通过深入Document内部对body进行检测,获取窗口大小
if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth)
{
winHeight = document.documentElement.clientHeight;
winWidth = document.documentElement.clientWidth;
}
callFlash();
}
function init(str)
{
window.onresize=findDimensions;
findDimensions();
//调用函数,获取数值
}
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
function callFlash() {
thisMovie("index").showSize(winWidth,winHeight);
}
</script>
</body>
</html>
as:
//设定一段时间之后初始化js函数 否则会出问题
var timer:Timer=new Timer(100,1);
timer.addEventListener(TimerEvent.TIMER,callJs);
timer.start();
function callJs(e:TimerEvent):void
{
ExternalInterface.call("init","ok");
}
//js调用as函数
ExternalInterface.addCallback("showSize",showSize);
//fit screen's width and height
function showSize(w:Number,h:Number):void
{
browser_w=w;
browser_h=h;
}//end