How can I set focus to my flash object?

1,316 views
Skip to first unread message

teshca

unread,
Dec 8, 2007, 10:13:08 AM12/8/07
to SWFObject
Hi. I am trying to migrate my application from SWFObject v1.5 to
SWFObject v2.0. I read documentation but I still don't get how can I
set focus to my flash object. I use swfobject.embedSWF, and then
document.getElementById(flashObjectId).focus(), but this rise
exception because element with that Id not yet exists. How can I be
sure that swfobject.embedSWF already finish its work and I can get dom
element? The simple setTimeout works but It is a kind of hack. Is
there a better solution for my problem?

Bobby

unread,
Dec 9, 2007, 5:44:08 AM12/9/07
to SWFObject
You can only call upon DOM elements when the DOM is loaded. Just use
the SWFObject 2.0 API [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_api_javascript_dev
] to execute the functionality after the DOM is loaded and after your
Flash content has been inserted, e.g. add the following code after
your embedSwf method:

function setFocusOnFlash() {
var fl = document.getElementById(flashObjectId);
if (fl) { fl.focus(); }
}
if (swfobject.hasFlashPlayerVersion("9.0.0")) { // insert the version
you test for
swfobject.addDomLoadEvent(setFocusOnFlash);
}

Just let me know if this works for you.

teshca

unread,
Dec 9, 2007, 12:25:11 PM12/9/07
to SWFObject
Thanks Bobby.
Your solution works fine for me.
I tried to do almost the same, but I used addLoadEvent instead of
addDomLoadEvent so it was't work without setTimeout hack.
Reply all
Reply to author
Forward
0 new messages