Press "Enter" to skip to content

Embed Flash in XHTML – Pass W3C Validation

Inserting flash(swf) files directly in XHTML causes w3c validation errors. This article explains how to embed flash in XHTML pages with out affecting the page Validation.
The Usual method is to use “<object>” tag and “<embed>” tag to insert the flash files in web pages. This  causes errors while validating the page as per the w3c standards.
This method uses a javaScript function to embed the flash objects in XHTML pages without affecting the page validation.

Step1

Insert the following javaScript code into the header of the page. (You may define this function in external JavaScript files also).

<script type="text/JavaScript">
 <!--// <![CDATA[
 function embedFlashObj(Embpath,Embwidth,Embheight){
 var flashCode = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+Embwidth+'" height="'+Embheight+'"><param name="movie" value="'+Embpath+'" /><param name="quality" value="high" /><embed src="'+Embpath+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+Embwidth+'" height="'+Embheight+'"></embed></object>';
 document.write(flashCode) ;
 }
 // ]]> -->
 </script>

NOTE:
Please remove the “/” [Short closing tag]  while using in non-XHTML pages .

Step2

Now, you need to call this function on the place where you need to get the flash file inserted.

<script type="text/javascript">
 // embedFlashObj('pathTOfile/file.swf',width,height);
 embedFlashObj('path/filename.swf',500,300);
 </script>

NOTE:
You may embed any number of flash files using this same function. Make necessary changes only to the script mentioned in the step2.
The function “embedFlashObj” requires 3 parameters sent along with the event, Path to the swf file, width of the Swf file and height of the swf file.
This will allow the web page to be validated against W3C standards.