Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

406 LESSON 14: Integrating Multimedia: Video and Sound


Embedding Flash Movies Using SWFObject


SWFObject is a combination of markup and JavaScript that provides a way to embed
Flash movies in web pages using standards-compliant markup that still supports all
the browsers that are currently in use. JavaScript is a programming language that runs
within the context of a web page, I explain how it works in more detail in Lesson 17,
“Introducing JavaScript.” You don’t need to know how to program in JavaScript to
use SWFObject; you just need to copy and paste some code and fill in a few blanks.
To download SWFObject and read the documentation, go to https://code.google.com/
archive/p/swfobject/.
Aside from providing a reliable way to present Flash movies using standards-compliant
markup, SWFObject also works around a problem that can’t be dealt with using markup
alone. When the version of the Flash player a user has installed is too old to play a
movie, the movie will not be presented, and any alternative content you provided will not
be displayed. The browser hands off the movie to the Flash player assuming it will work
and doesn’t handle things gracefully if it does not. SWFObject uses JavaScript to catch
these errors and show the correct alternative content when they occur.
SWFObject provides two approaches to embedding content; one uses markup augmented
by a bit of JavaScript (called the static publishing method), and the other uses pure
JavaScript (called the dynamic publishing method). Using markup provides better per-
formance and offers some level of functionality if JavaScript is disabled or the content is
republished in an environment where the JavaScript is not included. The dynamic version
is a bit more flexible in that it enables you to configure the embedded player on the fly.
Using SWFObject with markup requires three steps: adding the <object> tags, including
the swfobject.js file in the page, and registering the player with the SWFObject library.
First you should add the JavaScript to the <head> of your document:
<script src="swfobject.js"></script>
This loads the external JavaScript to the page. You’ll need to make sure that the src
attribute points to the correct location for your copy of swfobject.js. You’ll learn more
about JavaScript and external JavaScript files in Lesson 17.
Then you should embed your Flash player. Here’s the code:
<object id="myId" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
width="780" height="420">
<param name="movie" value="mymovie.swf" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash" data="mymovie.swf" width="780"
height="420">
Free download pdf