JavaScript lose focus when adding <embed>

I need to add a beep to my script. I add every time a sound should be.

$("#beep").html("<embed src='button-16.wav' hidden='true' autostart='true' loop='false' id='bp' />")

But when I add, my lost focus.

I tried to focus it again in the following ways.

1)

$("#beep").html("<embed src='button-16.wav' hidden='true' autostart='true' loop='false' id='bp' />");
$("#txt").focus();

2)

$("#beep").html("<embed src='button-16.wav' hidden='true' autostart='true' loop='false' id='bp' />");
$("#bp").ready(function(){
    $("#txt").focus(); 
});

But neither the first nor the second method works.

HTML code

<div id="beep"></div>
<input type="text" id="txt" />

So, how can I keep the focus of the text field after the beep?

+3
source share
2 answers

I found a completely different way to solve this problem. I wrote * .swf that play a sound when JavaScript calls its function.

ActionScript Code

package  {
import flash.display.*;
import flash.events.*;
import flash.system.*;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.external.ExternalInterface;

public class Main extends Sprite {
    var s:Sound;
    public function Main() 
    {
        ExternalInterface.addCallback("PlaySound", PlaySound);
        var request:URLRequest = new URLRequest("beep.mp3");
        s = new Sound(request);
    }
    public function PlaySound():void
    {
        s.play();
    }
}
}

JavaScript code.

$("#txt").keyup(function(event){
    if(event.which==13){
     var obj=thisMovie("Main");
     obj.PlaySound();
    }
});

function thisMovie(movieName) {
var movie;
try
{
    movie = document[movieName];
    movie = (movie == null) ? window[movieName] : movie;        
}
catch (e)
{
    return null;
}
return movie;
}
0
source

can you try setting the interval to see if the end of the wav file captures focus?

setInterval(function() {
  $("#txt").focus(); 
}, 100);

... . .wav, , ()

0

All Articles