spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / jf / column5 / 1 current pageTo page 2
[next]

JAVA Developer – Trading Industry (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow


JavaScript Popup Media Player

By Jonathan Fenocchi.

Playing videos in popup windows is often a simple task, but when you end up with multiple pages for each individual video, things can quickly become complicated. In this article, we’ll be discussing how to use just one page for your popup.

It’s easy to upload a video file and open it in a new window; it’s also easy to add a description by adding an HTML page, but that’s all static – you have to edit everything, and if you want to add another video, you have to create a new HTML file. Pretty soon, you’ll have a lot of unnecessary HTML documents. We can clean that up with JavaScript, and still make it accessible to the users without JavaScript. How? Have a look at this code:

<a href=”/media/file.mpg” onclick=”popMedia(0); return false;”>Play file.</a>

The direct link is to the actual media file – users without JavaScript can download the video, however, users with JavaScript will not go to where the link specifies due to the return false statement. At this moment, the popMedia() function is undefined. To define it, put the following in the <HEAD> tags of your document.

<script type=”text/javascript”><!—
  function popMedia(id){
   window.open(“player.html?f=”+id,”_popMedia”,”width=400,height=400”);
  }
//--></script>

The next step is to open a new window and add another HTML file. By having just one media player file, we won’t have to have the same JavaScript on every page. This way, you won’t have to modify any of your HTML files to add JavaScript to the header (even if you use the SRC attribute of the SCRIPT tag, you’ll have to modify all your HTML files). You can also directly link to the player.html file instead of having it open in a new window by using the following:

<a href=”/media/file.mpg” onclick=”location.href=’player.html?f=0’; return false;”>Play file.</a>

Users without JavaScript will link directly to the file; but users with JavaScript will be taken to the player.html file. Now I’m sure you’re curious about that zero being in there, when we should be specifying a file’s location. That can easily be explained by the source code of the player.html file below:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Media Player</title>
    <script type="text/javascript"><!--
      var files = ["file1.wmv","file2.avi","file3.mov"];
      var titles = ["File 1", "File 2", "File 3" ];
      var descs = ["This is the first file.",
                "This is the second file.",
                "This is the third file, which is a Quicktime file."
                ];
      var dims = ["400x400","400x400","400x400"];
      srchStr = location.search;
      obj = document.getElementById("medObj");
      srchStr = srchStr.split("f=")[1];
      objType = files[srchStr-1].split(".")[1];
      objType = (objType=="mpg"||objType=="mpeg")?"video/mpeg":
                (objType=="avi"||objType=="wmv") ?"video/x-msvideo":"video/quicktime";
var pluginspace = (objType=="video/x-msvideo")?"http://www.microsoft.com/windows/windowsmedia/default.aspx":(objType=="video/quicktime")?"http://www.apple.com/quicktime/download/":"";
   var codebase = (objType=="video/x-msvideo")?"http://www.microsoft.com/windows/windowsmedia/default.aspx":(objType=="vide/quicktime")?"http://www.apple.com/qtactivex/qtplugin.cab":"";
      document.title += " | "+titles[srchStr-1];
      dimW = Number(dims[srchStr-1].split("x")[0]);
      dimH = Number(dims[srchStr-1].split("x")[1]);
        window.resizeTo(dimW+300, dimH+100);
        window.moveTo (10 , 10);
    //--></script>
    </head>
   <body>
       <script type="text/javascript"><!--
if(navigator.appName != "Microsoft Internet Explorer") document.write('<obj'+'ect id="medObj" pluginspace="'+pluginspace+'" codebase="'+codebase+'" data="'+files[srchStr-1]+'" standby="Loading Video..." style="float: left;" type="'+objType+'" width="'+dimW+'" height="'+dimH+'"></object>');
if(navigator.appName == "Microsoft Internet Explorer") document.write('<embed pluginspace="'+pluginspace+'" src="'+files[srchStr-1]+'" style="float: left;" type="'+objType+'" width="'+dimW+'" height="'+dimH+'"></embed>');
   --></script>
   <div id="descObj" style="float: right; width: 200px;">
    <script type="text/javascript"><!--
      document.write (descs[srchStr-1]);
    //--></script>
   </div>
  </body>
</html>

As you can see, we’re placing arrays of corresponding data in our script. This way, the zero that comes after the f refers to the first index of each array, so that the file address, title, and description all correspond to one another. We also have a fourth array called “dims.” This array is what you’ll use to specify the dimensions of your video. You’ll notice that all three of the example files – file1.wmv, file2.avi and file3.mov – have different extensions. These are for demonstration purposes. Based on the video file’s extension, the script will detect the encoding type to tell the browser what plug-in to use to interpret the file. If the browser lacks any of the plug-ins, we’ve provided URL’s where the browser can download them, so you can view the videos.

home / programming / javascript / jf / column5 / 1 current pageTo page 2
[next]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark

Created: June 5, 2003
Revised: September 23, 2004

URL: http://webreference.com/programming/javascript/jf/column5/1