javascript cross browser element array like getElementsByTagName

Posted on 2007-04-01
Last Modified: 2013-11-19

I am trying to provide a number of audio files for streamin playback, the files are .swf. Since there will be multiple <EMBED> tags my 1st problem was that all tracks could be played at the same time, and despite setting the autostart property to false all of the embedded sounds played on page load. I read that to prevent this to use the following javascript in body onload: document.elementid.rewind(). This indeed worked and nothing plays now till is called.

Now, since the embed tags are dynamically generated there could be any number of them, i needed a way to stop any playing track before starting another and i wrote these functions:

function stop_all()
      //stop any other tracks
      var arrEmbedArray=document.getElementsByTagName("embed");
      for(var i=0;i<arrEmbedArray.length;i++)

function start_track(strEmbedID)
      //start selected track
      var objCurrentEmbed=document.getElementById(strEmbedID);

Really i wanted an "if" in the loopin stop_all() so the rewind is only called for the one playing track but i couldn't find a way so it is being called for each and every embed element, i'd like to change this but it works... Well, it's working in IE, but not in netscape, firefox, or opera. The alert in the loop was to test the loop was running and it isnt running in any other browser than IE.

I read that getElementsByTagName is supported as follows: IE v5 +, Firefox v1 + and opera v9 +. I think i may need to do things differently for these browsers, hope someone can help! thanks all.

Question by:s1desh0w
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 26

Expert Comment

ID: 18834238
Why don't you keep the current track and change like this:

var CurrentEmbedID = '';

function stop_track()
       if (CurrentEmbedID != '')
         CurrentEmbedID = '';

function start_track(strEmbedID)
      //start selected track
      CurrentEmbedID = strEmbedID;

Author Comment

ID: 18836123
yes, thanks, i did think of keeping the current track as a global var after making the post, i havnt tried it yet, i will let you know if that works, more importantly will this work cross browser because there is some problem in all browsers except ie that is preventing the code in my for loop from running (getElementsByTagName). In addition all tracks will play onload unless i call .rewind() for each one onload as well, thats what stop_all() is doing, so i should keep this function as is?

thanks for your help
LVL 26

Accepted Solution

DireOrbAnt earned 125 total points
ID: 18838511
I see now, are those Flash movies? getElementsByTagName works on IE and all others. The issue here is that IE will "hide" the embed tag from the DOM tree if it's inside an object tag (like flash movies should be if you use embed). Another option is to use getElementsByClassName, get the function here:

And the give all embeds AND objects a class="MyClips" or something...

Author Comment

ID: 18854725
i've used a workaround to get round this problem by using the following javescript to write the flash object to a <div> from an onclick event: It saves having to stop the tracks on load and on play.

function start_track(strEmbedSource)
      objLayer.innerHTML='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase=",0,19,0" WIDTH=0 HEIGHT=0><PARAM NAME=movie VALUE="' + strEmbedSource + '" /><PARAM NAME=loop VALUE=false /><PARAM NAME=volume VALUE=100 /><PARAM NAME=quality VALUE=high /><EMBED src="' + strEmbedSource + '" loop=false volume=100 quality=high width=0 height=0 hidden=true type="application/x-shockwave-flash" pluginspage=""></embed></object>'

the only problem i have remaining is that in the same way as setting <param name=autostart value=false> doesnt prevent these files from playing on load, setting <param name=loop value=false> doesnt prevent the track from looping either. as you can see from the code above i have included these values in both the param name and the embed tag, the files by the way are only audio but converted from mp3 to swf at a lower quality to save bandwidth on streaming, i used MP3 Stream trial version to do this, thanks again for ure help.

Author Comment

ID: 18854728
p.s. i will award the points for your previous answer tho since i believe it would solve my original problem. cheers

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question