shutat Posted October 17, 2008 Share Posted October 17, 2008 I'm working on a game where I have a script that polls the keyboard in order to simulate movement of a central character. If the character is at a certain coordinate, I change the src property of an iframe to load a midi file. That's where my problem arises, I lose focus on the character and cannot figure out programmatically how to get it back. Is it possible? Here's a basic idea of what I'm up to. main.html <head> <script language="javascript"> var x, y; function pollKeyB(e) { ... if(x == m && y == n) { document.getElementById("midi").src = "sndwrap.php?snd=" + snd_file; } ... </head> <body onload="init()" onkeyup="pollKeyB(event)"> <iframe id="midi" src="sndwrap.php"></iframe> <table id="main" ...> game ui </table> </body> ... I've tried everything from setting the iframe's onfocus event to main's onblur event, but none of the combinations I've tried works. in sndwrap.php I get the sound file via $_GET and then set either an embed or MS's function (forget it's name) to play the midi file. I even tried setting sndwrap's body onfocus event to blur, but that didn't work either. It seemingly *works* but in order to move the character again, I have to click inside of the table "main." Does anyone have any ideas how I keep focus on the keyboard polling, yet still be able to dynamically change sound via the iframe src? I'm not that familiar with JS other than a few docs I've read. As a side question, what is purpose of returning true or false in some functions? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 17, 2008 Share Posted October 17, 2008 You can use document.getElementById("main").focus() to set the focus back onto it. Quote Link to comment Share on other sites More sharing options...
shutat Posted October 17, 2008 Author Share Posted October 17, 2008 You can use document.getElementById("main").focus() to set the focus back onto it. Thanks for the reply - I sure appreciate it. I tried that in the init function and as a stand alone function for main's onblur event, but it didn't work - I either got .focus() isn't a function in the js console or it didn't do anything when I used .focus Where should it go? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.