JavaScript EditorBest javascript editor debugger     Ajax website 

Main Page

Previous Page
Next Page

Using JavaScript to Call a Movie

Apple currently recommends using JavaScript to call movies. It all started because a company called Eolas sued Microsoft for making ActiveX components automatic. In January of 2006, Microsoft changed IE so that users had to click to activate ActiveX controls (like the QuickTime plugin). Although QuickTime movies should play in the background even without clicking (like Windows Media Player files do), they don't. Apple found that you can get QuickTime movies to play without user activation if you call the movie with JavaScript as shown here.

To use JavaScript to call a QuickTime movie:

Create a new text file for your JavaScript script.

In the JavaScript text file, type function InsertSampleMovie( ) { to begin the script.

Next, type document.write('html');, where html is the code for your movie (e.g., the object elements as described on pages 286291) with absolutely no carriage returns (Figure 18.16).

Figure 18.16. Here is the minimalist JavaScript: define the function, write out all the (X)HTML code for the movie, including any nested objects and conditional comments, being sure to omit all carriage returns and single quotes, and then close the function with a }.

Finally, type } to complete the script.

Save the file in text-only format with the .js extension.

In the head section of your Web page, type <script src="movie.js" type= "text/javascript"></script>, where movie.js is the location on the server of the script that you created in steps 15.

In the place on your Web page where you want the movie to be embedded, type <script language= "JavaScript" type= "text/javascript" > InsertSampleMovie( ); </script>.


  • The method for inserting QuickTime movies that I give earlier in this chapter works fine for QuickTime movies except if you want them to autoload in IE 7 for Windows. In that case, I recommend going the JavaScript way. The JavaScript method also saves visitors from having to click to activate the ActiveX control.

  • About 6% of Internet users currently surf without JavaScript (see Those folks won't be able to view movies called with this method.

  • If you'd rather not make one long endless line in step 3, you can divide the (X)HTML code up into various lines of JavaScript code. Each line of JavaScript code must start on its own line, begin with document.write(' (no space, left parenthesis, single quote), followed by any amount of (X)HTML code, including white space but absolutely no returns or single quotation marks, followed by the '); (single quote, right parenthesis, semicolon) and a return (Figure 18.17).

    Figure 18.17. If you like, you can create multiple document.write lines for each (X)HTML element. Be careful with your punctuation (see tip 3).

  • According to Microsoft, multimedia in ActiveX controls that requires no interaction should continue to function. That is, QuickTime movies should be able to load in the background and autoplay with no clicking required. Unfortunately, that does not happen in Internet Explorer for Windows (I'm getting tired of typing "surprise!") even though it does work like that for Windows Media Player files. Seems like a conspiracy to me.

    Figure 18.18. In the Web page, you have to load the script in the head section and then call the function where you want the movie to appear.

  • For inserting multiple movies, see Or even better, Geoff Stearns' SWFObject:

    Figure 18.19. The movie will open in all browsers with JavaScript activated and will autoplay even in Internet Explorer for Windows.

Previous Page
Next Page

JavaScript EditorBest javascript editor debugger     Ajax website