明凯博客

关注网站技术,一个特立独行的程序员

SWFObject的基本使用方法

swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes)有5个必须的参数和4个可选的参数:

1
2
3
4
5
6
7
8
9
swfUrl(String,必须的)指定SWF的URL。
id(String,必须的)指定将会被Flash内容替换的HTML元素(包含你的替换内容)的id。
width(String,必须的)指定SWF的宽。
height(String,必须的)指定SWF的高。
version(String,必须的)指定你发布的SWF对应的Flash Player版本(格式为:major.minor.release)。
expressInstallSwfurl(String,可选的)指定express install。
flashvars(String,可选的)用name:value对指定你的flashvars。
params(String,可选的)用name:value对指定你的嵌套object元素的params。
attributes(String,可选的)用name:value对指定object的属性。

例如:

1
2
3
4
5
6
7
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF("myflash.swf", "myflash", "300", "120", "9.0.0");
swfobject.embedSWF("myflash.swf", "myflash", "300", "120", "9.0.0", 
null, null, {wmode: 'transparent'});
</script>
<div id="myflash">这里是当flash不能正常显示时显示的内容</div>

怎样配置你的Flash内容?
你可以为你的object元素添加下面这些常用的可选属性(attributes):

1
2
3
4
id
name
styleclass(不使用class,因为class也是ECMA4的保留关键字)
align

你可以使用下面这些专用于Flash的可选param元素:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
play
loop
menu
quality
scale
salign
wmode
bgcolor
base
swliveconnect
flahvars
devicefont
allowscriptaccess 
seamlesstabbing 
allowfullscreen 
allownetworking

怎样用JavaScript对象来定义你的flashvars、params和attributes?
你最好用对象的字面量(Object literal notation)来定义JavaScrpt对象,例如这样:

1
2
3
4
5
6
7
<script type=“text/javascript“>
var flashvars = {};
var params = {};
var attributes = {};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", 
flashvars, params, attributes);
</script>

你可以在定义对象的时候添加你的name:value对(注意:请确保不要再对象中的最后一个name:value对后面加上逗号):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script type=“text/javascript“>
var flashvars = {
name1: "hello",
name2: "world",
name3: "foobar"
};
var params = {
menu: "false"
};
var attributes = {
id: "myDynamicContent",
name: "myDynamicContent"
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", 
flashvars, params, attributes);
</script>

或者在对象创建之后用点号添加属性(properties)和值:

1
2
3
4
5
6
7
8
9
10
11
12
13
<script type="text/javascript">
var flashvars = {};
flashvars.name1 = "hello";
flashvars.name2 = "world";
flashvars.name3 = "foobar";
var params = {};
params.menu = "false";
var attributes = {};
attributes.id = "myDynamicContent";
attributes.name = "myDynamicContent";
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", 
flashvars, params, attributes);
</script>

上面的代码也可以这样写:

1
2
3
4
5
6
<script type="text/javascript">
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf",
 
{name1:"hello",name2:"world",name3:"foobar"}, {menu:"false"}, 
{id:"myDynamicContent",name:"myDynamicContent"});
</script>

如果你不想使用一个参数,你可以将它定义为false或者一个空对象:

1
2
3
4
5
6
7
8
9
10
<script type="text/javascript">
var flashvars = false;
var params = {};
var attributes = {
id: "myDynamicContent",
name: "myDynamicContent"
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf",
 flashvars, params, attributes);
</script>

flashvars对象是一个为了增加易用性而设计的作为快捷方式的参数,所以你可以先忽略它,然后在params对象中指定你的flashvars:

1
2
3
4
5
6
7
8
9
10
11
12
13
<script type="text/javascript">
var flashvars = false;
var params = {
menu: "false",
flashvars: "name1=hello&name2=world&name3=foobar"
};
var attributes = {
id: "myDynamicContent"
name: "myDynamicContent"
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", 
flashvars, params, attributes);
</script>

, , , ,

相关文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注