Use SWFObject to perfectly solve the browser compatibility problem of inserting Flash into HTML

Use SWFObject to perfectly solve the browser compatibility problem of inserting Flash into HTML

Let’s learn together

1. Traditional methods


Copy code
The code is as follows:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"codebase="<a href="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0</a>" width="550" height="400" id="Untitled-1" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="mymovie.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="mymovie.swf" quality="high" bgcolor="#ffffff" width="550" height="400"name="mymovie" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="<a href="http://www.macromedia.com/go/getflashplayer">http://www.macromedia.com/go/getflashplayer</a>" />
</object>

This method uses the object and embed tags to embed. If you are careful, you will find that many parameters of the object and many attributes in the embed are repeated. Why is this done? For browser compatibility, some browsers support object and some support embed, which is why you need to modify both places when modifying Flash parameters.

This method is the official method of Macromedia, which guarantees the functionality of Flash to the greatest extent and has no compatibility issues. But it doesn't work so well now: it fails to validate because the embed tag embedded for compatibility does not comply with the W3C specification. Of course, if you don’t care about standards or not, that’s another matter.

For various reasons, Microsoft restricted the usage mode of IE's ActiveX after SP2. That is, there is a virtual box in the ActiveX on the page, and the user needs to click once to interact normally. Flash is embedded into the web page as an ActiveX, so it will also be implicated. This problem can only be solved by embedding Flash through JS.

There is no Flash version detection. If the browser's flash plug-in version is not enough, your swf file may not be displayed normally, or an ActiveX confirmation installation box may pop up - this box is very scary for many users.

2. Method of embedding with JS

When embedding with JS, there are different embedding methods, some are good and some are not. Some people use document.write to write directly. To be honest, this method is not very good. It feels like it has too many hack elements and is a bit like verification for the sake of verification. It also does not reflect the advantages of JS. I think a good JS embedding script, while ensuring the functions that Flash should have, should give full play to the advantages of JS. It should have version detection, be able to solve accessibility issues well (that is, what to do when users cannot browse Flash content or disable JS), and be easy to reuse.

What we are going to talk about here is the SWFObject solution:

"SWFObject" uses Javascript to insert flash, which has many advantages. The code is concise, there will be no "Click here to activate the control" prompt under IE6, and it can pass W3C verification. Different from the traditional method of inserting "object" into flash.

In the new 2.x version of SWFObject, the simplest call only requires one sentence, and there is no need to wait for the page to load, which means you can write this sentence anywhere on the page. It is much simpler than the previous version. Here are some simple and commonly used calling methods:

1. The simplest and most basic classic sentence that can be used whenever you want to insert flash.


Copy code
The code is as follows:

<div id="swfid"></div>
<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>

Note: Call the embedSWF method to insert the SWF file. The parameters are: @the address of the swf file; @the id of the container (such as div) used to load the swf file; @the width of the flash; @the height of the flash (of course, the width and height here can be expressed as percentages such as 100%); @the minimum version required to play the flash normally; @when the version is lower than the requirement, execute the swf file. Here, use this flash to jump to the official download of the latest version of the flash plug-in. (This parameter can be omitted) When inserting multiple flashes into different locations on the same page, just repeat the above statement and use different container IDs.

2. Calling methods to pass parameters, variables, and properties to swf files


Copy code
The code is as follows:

<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
//1. Use Json to initialize variables, parameters, and properties
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); </p> <p>//2. Traditional initialization settings, the effect is the same
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. Write it directly at the end, just one sentence, concise and powerful, without any procrastination
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>

SWFObject 2.0 official documentation (Chinese) https://www.jb51.net/books/175630.html

github: https://github.com/swfobject/swfobject .

<<:  Detailed explanation of the differences between the four types of positioning in CSS

>>:  Analysis of the process of publishing and deploying Spring Boot applications through Docker

Recommend

How to add configuration options to Discuz! Forum

Discuz! Forum has many configuration options in th...

Vue implements tree table through element tree control

Table of contents Implementation effect diagram I...

A simple method to regularly delete expired data records in MySQL

1. After connecting and logging in to MySQL, firs...

Linux C log output code template sample code

Preface This article mainly introduces the releva...

HTML multi-header table code

1. Multi-header table code Copy code The code is a...

An article to show you how to create and use Vue components

Table of contents 1. What is a component? 2. Crea...

Vue network request scheme native network request and js network request library

1. Native network request 1. XMLHttpRequest (w3c ...

Solution to win10 without Hyper-V

Are you still looking for a way to enable Hyper-v...

How to understand the difference between computed and watch in Vue

Table of contents Overview computed watch monitor...

...

How to copy MySQL table

Table of contents 1.mysqldump Execution process: ...

Summary of common tool examples in MySQL (recommended)

Preface This article mainly introduces the releva...

MySQL5.7 parallel replication principle and implementation

Anyone who has a little knowledge of data operati...

Vue page monitoring user preview time function implementation code

A recent business involves such a requirement tha...

How to avoid data loop conflicts when MySQL is configured with dual masters

I wonder if you have ever thought about this ques...