4

Is there a way to set focus to the embed HTML element using JavaScript? Test case: embedded YouTube videos on a page.

I have no control over the embedded Flash element. So, is there a way to set focus on it by using only JavaScript?

I read somewhere that calling the element.focus() method works only in IE. I need a browser-independent way that works in Chrome/Firefox.

Thanks!

ankit
  • 1,273
  • 3
  • 13
  • 12

4 Answers4

2

This only works in Internet Explorer.

http://kb2.adobe.com/cps/155/tn_15586.html

jbrown
  • 21
  • 2
0

I haven't confirmed this, but you could try:

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
    } else {
         return document[movieName];
    }
}

    // Call from within another function:
    thisMovie("FlashObjectID").focus();
    thisMovie("FlashObjectID").showFlash(); 
    // showFlash() is an AS3 ExternalInterface call from JS to .swf which establishes the TextInput.setFocus(); method

source: http://www.htmlforums.com/archive/index.php/t-64150.html

posit labs
  • 8,951
  • 4
  • 36
  • 66
0

I've tried to do this too, and ended up to a nice solution using jquery:

var gotoflash=jQuery("#flash_file").offset().top;jQuery("html:not(:animated),body:not(:animated)").animate({ scrollTop: gotoflash}, 1000);

where: <div id="flash_file"> flash object code here </div>

Manolis
  • 57
  • 1
  • 1
  • 3
  • I tried this. But as far as I can comprehend, this is meant to scroll the page to the flash object. I actually want to set focus to the element. So that, for eg., in YouTube, after setting focus to the element, I can use the keyboard shortcuts like "space" for playing/pausing the video – ankit Dec 28 '09 at 14:36
  • hm..i understood... try this,it works: ,where OBJECTID is: ... – Manolis Jan 07 '10 at 21:15
0

It can be done by adding flash content dynamically, for example with swfobject.

korda
  • 804
  • 7
  • 18