[分享] 使用 SWFObject.js 插入Flash且符合W3C标准
“SWFObject”是利用Javascript 插入flash,好处多多,代码简洁,不会出现IE6下的“单击此处以激活控件”的提示,并且能通过W3C验证。不同于传统的“object”插入flash的方法。
SWFObject在新的2.x版本中,其最简单的调用竟只需一句话,并且不需要等待页面加载完成,这意味着你可以将这句话写在页面的任何地方。比以前的版本,要简便多了。下面来看几个简单常用的调用方法:
1、最简单,最基本,只要想插入flash都能用到的经典一句话。
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF("test.swf", "swfid", "300", "120", "9.0.0", "expressInstall.swf");
</script>
<div id="swfid"></div>
<script type="text/javascript">
swfobject.embedSWF("test.swf", "swfid", "300", "120", "9.0.0", "expressInstall.swf");
</script>
<div id="swfid"></div>
参数注解:调用方法embedSWF——插入SWF文件,参数依次是:
@swf文件的地址;
@用于装入swf文件的容器(如div)的id;
@flash的宽度;
@flash的高度(当然,这里的宽高都可以使用诸如100%这样的百分比来表示);
@正常播放该flash所需的最低版本;
@当版本低于要求时,执行该swf文件,这里利用这个flash跳转到官方下载最新版本的flash插件。(该参数可以省略)
在同一个页面插入多个flash到不同位置时,只要重复上面的语句,使用不同的容器id就可以了。
2、给swf文件传递参数、变量、属性的调用方法
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
//1、使用Json初始化变量、参数、属性
var flashvars = {
name1: "hello",
name2: "world",
name3: "foobar"
};
var params = {
menu: "false"
};
var attributes = {
id: "dynamicContent2",
name: "dynamicContent2"
};
swfobject.embedSWF("test6_flashvars.swf", "content2", "300", "120", "6.0.0", "expressInstall.swf", flashvars, params, attributes);
//2、传统的初始化设置,效果一样
var flashvars = {};
flashvars.name1 = "hello";
flashvars.name2 = "world";
flashvars.name3 = "foobar";
var params = {};
params.menu = "false";
var attributes = {};
attributes.id = "dynamicContent3";
attributes.name = "dynamicContent3";
swfobject.embedSWF("test6_flashvars.swf", "content3", "300", "120", "6.0.0", "expressInstall.swf", flashvars, params, attributes);
//3、直接写在后面,就一句话,简洁剽悍,不拖泥带水
swfobject.embedSWF("test6_flashvars.swf", "content5", "300", "120", "6.0.0", "expressInstall.swf", {name1:"hello",name2:"world",name3:"foobar"}, {menu:"false"}, {id:"dynamicContent5",name:"dynamicContent5"});
</script>
<script type="text/javascript">
//1、使用Json初始化变量、参数、属性
var flashvars = {
name1: "hello",
name2: "world",
name3: "foobar"
};
var params = {
menu: "false"
};
var attributes = {
id: "dynamicContent2",
name: "dynamicContent2"
};
swfobject.embedSWF("test6_flashvars.swf", "content2", "300", "120", "6.0.0", "expressInstall.swf", flashvars, params, attributes);
//2、传统的初始化设置,效果一样
var flashvars = {};
flashvars.name1 = "hello";
flashvars.name2 = "world";
flashvars.name3 = "foobar";
var params = {};
params.menu = "false";
var attributes = {};
attributes.id = "dynamicContent3";
attributes.name = "dynamicContent3";
swfobject.embedSWF("test6_flashvars.swf", "content3", "300", "120", "6.0.0", "expressInstall.swf", flashvars, params, attributes);
//3、直接写在后面,就一句话,简洁剽悍,不拖泥带水
swfobject.embedSWF("test6_flashvars.swf", "content5", "300", "120", "6.0.0", "expressInstall.swf", {name1:"hello",name2:"world",name3:"foobar"}, {menu:"false"}, {id:"dynamicContent5",name:"dynamicContent5"});
</script>