views:

60

answers:

3

Suppose the swf file is embeded into the page with the following code:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="myFlash" width="600" height="500">
   <param name="movie" value="myMovie.swf">
   <embed type="application/x-shockwave-flash" src="myMovie.swf" name="myFlash" width="600" height="500" >
   </embed>
</object>

What are the ways to get a reference to the movie with the help of JavaScript?

A: 

Does...

var myReference = document.getElementById("myFlash");

... suit your needs? What do you aim to do with this reference once you are done?

LeguRi
I believe it won't work for the embed tag 'cause it doesn't have the id attribute.
WeWillFlashYou
+2  A: 
function getMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

var flash = getMovie('myFlash')
hobodave
I`m curious... how is this different from or better than `document.getElementById("myFlash")`?
LeguRi
It's different in that it will always work. getElementById is specific to using swfobject.js, I believe.
hobodave
Hmmm... after further reading, apparently getElementById works for the <object> tag but in browsers such as IE it's actually the <embed> tag which contains the movie... interesting.
LeguRi
So am I very curious... First of all, browser detection via client string is probably not a very good practice, is it? And aslo, document[movieName] works in IE too (I just tested).
WeWillFlashYou
On this page http://developer.longtailvideo.com/trac/wiki/Player5Api they suggest to place the id attribute in the embed tag too (along with the name attribute) and then get a reference to the movie the way Richard suggested. As I just tested, it won't work. What's the matter?
WeWillFlashYou
In general, no that is not a good practice, but it's standard for what you desire. This is one of those areas of browsers that everyone sucks at. See http://www.permadi.com/tutorial/flashGetObject/
hobodave
Hey, Richard, it is the browsers that use plug-ins (not activeX) work via the embed tag. IE use activeX.
WeWillFlashYou
hobodave, thanks for info. But don't you think referencing to the object via document[object] is all what's needed? As it works in all the browsers I am fortunate to test on.
WeWillFlashYou
@WeWillFlashYou: Honestly, I'm not sure. I don't know what you've tested on. However, I've found that using anything but the above has had edge cases where things didn't work as expected. Are you supporting IE6? IE6 has always been a requirement for the apps I develop, thus I have to do assy things to get it to work.
hobodave
I get a reference to the flash object successfully just via document[flashObject]. No need for browser detection. Have just tested with Mozilla Firefox, Opera, Google Chrome, SeaMonkey, Safari, Netscape Navigator and Internet Explorer 6. All on Windows OS.
WeWillFlashYou
Ha! Ha! The article hobodave pointed to just says the contrary of what's on the page I metioned (http://developer.longtailvideo.com/trac/wiki/Player5Api). It claims that assigning of the id attribute to the <embed> tag should be avoided.
WeWillFlashYou
Heh, I say just use what works. :P
hobodave
That is exactly what I have been looking for. :P
WeWillFlashYou
@WeWillFlashYou - Ya, I got the IE thing backwards :S but don't give the same 'id' to your <object> and your <embed> - id attributes have to be unique within your HTML document.
LeguRi
Exactly! Same ids won't work together within one document. BTW, I concluded that document[oSWF] works just like a charm across browsers. No need for browser detection. I hope things are also well on other platforms. If somebody got something on the conundrum, plz drop a line here or mail me to [email protected]
WeWillFlashYou
@WeWillFlashYou: how about accepting my answer? :P
hobodave
Well, it will cost you $5... Contact to my manager.
WeWillFlashYou
:-P Seriously though, are you waiting for a better answer?
hobodave
A better answer? I have already answered my question. Just thought people knew what I didn't... Oki, flash forever!!!
WeWillFlashYou
A: 

It's easy, but you need to be weary of Internet Explorer

var myFlash = $.browser.msie ? window[ 'myFlash' ] : document[ 'myFlash' ];

Horia Dragomir
You answer is jquery specific, the question doesn't say anything about jquery.
hobodave
I agree with @hobodave. You need to at least mention that you are using jQuery. The way you present it it looks like a normal browser feature. Additionally, using `$.browser` is being discouraged by the jQuery project and they recommend `$.support` instead.
Doug Neiner
jQuery is for preps!
WeWillFlashYou