PDA

View Full Version : Can you Auto-Focus the Flash Object?


sfhazel
10-24-2006, 10:26 PM
I am using an onMouseWheel listener in my movie. In order for this to work, the user has to click the movie first. I'm talking about basic focus, not that silly gray rectangle (I used the javascript trick to make that part go away). Anyway, this movie is the only thing on the website, and I'd like to just use a javascript to set focus on the movie so that the mousewheel effect works right off the bat. I tried giving giving the flash object the id name "flash" and doing this for my onload command:

<body onload="document.getElementById('flash').focus();">

My javascript debugger shows this syntax as being correct. But, even though it's "setting focus" on the flash object, I still have to click first.

Edit: This WORKS in IE, but fails in Firefox. Anyone know a workaround?

edtsch
10-26-2006, 07:57 PM
It may be that within Firefox/Mozilla your getElementById reference is not pointing to the right element in the DOM. Is your NAME attribute the same as your ID?

I had a similar situation where Firefox wasn't resolving a reference I was trying to make and I ended up using the following HACK to provide a pointer to the movie:

function thisMovie(movieName) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
if (isIE) {
return window[movieName];
} else {
var embeds = document.body.getElementsByTagName('embed');
return embeds.item(0);
}
// function was:
// return (isIE) ? window[movieName] : document[movieName];
}

I hate to recommend a hack like this, but since there won't be any other embeds on the page, it might work for you. Anyway, you might find with Firefox that getElementById doesn't work for you, but getElementsByTagName does.

sfhazel
10-27-2006, 05:17 AM
Firefox is getting the correct element. I tested this by replacing the code with this:

document.getElementById('flash').style.backgroundC olor = "red";

This succesfully changed the background color to red.

After doing some more research, I read on more that one website that this focus method only works in IE (something to do with how the embedded movie is handled in the netscape family of browsers). Unless someone specifically knows a workaround, I guess I will have to settle for the user needing to click the Flash movie before mouse scroll effects work.

jsebrech
10-27-2006, 07:12 AM
This is why all flash movies contain a "play" button (on sites like youtube for example). The click on "play" focuses the movie.

edtsch
10-27-2006, 05:12 PM
Firefox is getting the correct element. I tested this by replacing the code with this:

document.getElementById('flash').style.backgroundC olor = "red";

This succesfully changed the background color to red.
It may be that you've successfully targeted the div or span that the swf is in, but not the movie itself. That's what I was getting at with trying to target the embed tag directly.

In my case, I found that in order to invoke a Flash function (a la ExternalInterface) within the SWF from javascript, I had to use the wacky embed targetting because the other wasn't working.

You may be right that this focus() method is not available for certain OS/plugin combos. I'm just saying it might be worth a try.

edtsch
10-27-2006, 05:34 PM
got this from another site as a cross-browser way to target a flash movie via the DOM:

function getFlashMovieObject(movieName)
{
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}

As you can see, the getElementById method only works for IE.

absolutezero
11-02-2006, 12:21 PM
sfhazel did you ever find a solution for this?

the binary
11-02-2006, 12:45 PM
hi

iŽve heard (not tested) youŽll have to write the complete 'flash-relating'-code
with javascripts "document.write()" to avoid this..

my 2 cents ;)

sfhazel
11-02-2006, 11:26 PM
hi

iŽve heard (not tested) youŽll have to write the complete 'flash-relating'-code
with javascripts "document.write()" to avoid this..

my 2 cents ;)

That solution is for the "gray rectangle" problem. (A practice I currenly implement). What I'm talking about setting focus to the flash object so that you do not need to click inside it to activate all the functions, specifically the mousewheel functionality that I use in my movie.

sfhazel
11-02-2006, 11:48 PM
Let me elaborate more on my code, so that everyone can better understand what I'm trying to accomplish, exactly. I'm using an XML 1.0 Strict doctype. This is my javascript code that I use to embed my movie:

document.write( '<object' +
' id="flash" name="flash"' +
' type="application/x-shockwave-flash"' +
' data="c.swf?path=layout.swf" ' +
' width="760" height="630">' +
'<param name="movie" value="c.swf?path=layout.swf" />' +
'</object>');

I can use the following code to make my Flash movie disappear. This works in both IE and Firefox:

document.getElementById("flash").style.display = "none";

This is why I have assumed that getElementById works. So I change that code to look like this:

document.getElementById("flash").focus();

This works for IE browsers. The object gets focus and my mouewheel movements work without ever having to click the Flash movie. This does not work in Firefox, and is why I assumed that the focus() function does not work to target the <object> tag in Netscape.

PS: Edtsch, I tried the function that you provided. It worked for IE, but not for Firefox.

Moving on, I wanted to be sure that the problem was not the focus() function, so I tried this:


document.getElementById("flash").onkeypress = function(){ alert(this); }
document.getElementById("flash").focus();

I refresh my page, and press a key. What do you know, an alert() pops up. So obviously the <object> tag received focus. So, for some reason in Netscape/Firefox browsers, focusing on the <object> tag does not mean that the active-x/Flash content gets focus. I've been playing around with the DOM documentation for a while, and I haven't yet been able to sneak around this problem. Google searches have come up short, as well.

Hoogs
04-03-2008, 05:27 AM
I think the problem is a little more sinister...
onMouseWheel Just doesn't register in my windows Firefox or Safari.
mouseListener = new Object();
mouseListener.onMouseWheel = function(delta:Number) {
_root.test_txt.text = "delta = "+delta;
};
Mouse.addListener(mouseListener);
In IE it works fine but not the other two.

Anyone know anything about this?

FormerSwinger
04-16-2008, 01:51 PM
it's not about the mousewheel not registering in Firefox (or at least not there is more to it)

I have a simple swf with a hover color change effect. All works well until I scroll the page on FF. After the scroll most swf instances lose focus and will only gain it after a user clicks on them.

I've tried every way that I know of to access the swf and set focus to no avail.

Any ideas would be appreciated.

It's a nice change to have something working in IE but not in FF though :D

Paulskii
05-13-2008, 07:47 PM
got this from another site as a cross-browser way to target a flash movie via the DOM:

function getFlashMovieObject(movieName)
{
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}

As you can see, the getElementById method only works for IE.


I know this may be ages old...but how would you apply the code above to force the focus?