Computing.Net > Forums > Programming > Set focus for JavaScript

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Set focus for JavaScript

Reply to Message Icon

Name: Larry21
Date: August 1, 2004 at 14:27:41 Pacific
OS: Win XP H.E.
CPU/Ram: P4 2.6+HT 512MB
Comment:

Hi,
I'm using a window with frames and in one of the frames there are functions that are triggered with 'onkeydown' and 'onkeyup'. Everything was working just fine, and now no matter what I do, the events will not happen automatically on load unless I first click somewhere in the frame. I have tried 'self.focus()' and 'parent.left.focus()' and other things and nothing seems to get the key presses registered automatically when the page loads without my first clicking somewhere.

A way around it that works is to create a visible form and have focus set to it, and then all key presses are registered- but then there's an unneccessarry visible form (and it cant be made invisible because then it wont allow focus... I think)

If anyone has any ideas I'd greatly appreciate it,
Larry

PS- I honestly have no idea what happened because it would all along automatically register any key presses after loading, but now after my having runs some scripts or I dont know what else I've done on this system, it doesnt focus right automatically...



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: August 1, 2004 at 22:29:29 Pacific
Reply:

The focus() method seems to be available only for form elements and the window object. So the parent.left.focus() wouldn't work because parent.left doesn't support that method.

If you can post a link to the offending page that would be helpful...But one possible workaround would be to play with making a form element that does support the focus() method hidden by changing it's style. Something like this:

<SCRIPT>
function focus_and_hide() {
document.getElementById('test').focus();
document.getElementById('test_container').style.display="none";
}
</SCRIPT>
<BODY onload="focus_and_hide();">
<FORM>
<DIV id="test_container">
<INPUT type='text' id="test" name="mytest">
</DIV>
</BODY>

Of course, hiding the element may make the form/frame lose focus, so it may not work. Something to think about though.

Good Luck!
-SN


0

Response Number 2
Name: Larry21
Date: August 2, 2004 at 18:32:16 Pacific
Reply:

But what I have found is that being that the actual form is hidden, it will not allow focus there!

Right now what I've done is told the actual frame (ie the html in the frame) to focus on itself onload (document.body.focus()) This seems to work, though I think that it should have focus automatically like it used to... I have no clue what happened...)

Thanks for your help though (you also helped me before to start my script, and it has since evolved a lot...)

Thanks again,
Larry

PS- For reference, this is what I started with: http://computing.net/webdevel/wwwboard/forum/708.html

And this is what I'm with right now (after learning a thing or two on the way... Though I'm sure that there's a much more efficient way to do this, as of now it works so I dont mind all the extra code...):

<HTML>
<HEAD>
<TITLE>RC</TITLE>
<script type = "text/javascript">

var lngIdleTimer;
var intNestCount = 0;
var arActiveKeys = new Array();

function Idle(){
clearTimeout(lngIdleTimer);
off()
}
function Trace(message){
document.getElementById("Trace").innerHTML += message;
}
function RegisterKeyDown(){
var Index = event.keyCode;

clearTimeout(lngIdleTimer);
if ( arActiveKeys[Index] == undefined ){
arActiveKeys[Index] = true;
intNestCount++;

}

}
function RegisterKeyUp(){
var Index = event.keyCode;

if ( arActiveKeys[Index] != undefined ){
arActiveKeys[Index] = undefined;
intNestCount--;
if ( intNestCount == 0 )
lngIdleTimer = setTimeout("Idle()", 0);
}

}

var upkeydown=false
var downkeydown=false
var rightkeydown=false
var leftkeydown=false
var shiftkeydown=false
var uprightallow=true
var upleftallow=true
var downrightallow=true
var downleftallow=true
var shiftupallow=true
var upallow=true
var downallow=true
var rightallow=true
var leftallow=true
var shiftallow=true

function keydown()
{
var keypress=window.event.keyCode;
if (keypress==38) {upkeydown=true;RegisterKeyDown()}
if (keypress==40) {downkeydown=true;RegisterKeyDown()}
if (keypress==39) {rightkeydown=true;upallow=false;downallow=false;}
if (keypress==37) {leftkeydown=true;upallow=false;downallow=false;}
if (keypress==16) {shiftkeydown=true;upallow=false;downallow=false;}
//To have right left and shift trigger off, add downif() in their if's above
verify()
}

function verify()
{
if ((uprightallow==true) && (upkeydown==true) && (rightkeydown==true)) {parent.right.document.keyform.direction.value="upright";parent.right.document.keyform.submit();uprightallow=false;upallow=false;rightallow=false;}
if ((upleftallow==true) && (upkeydown==true) && (leftkeydown==true)) {parent.right.document.keyform.direction.value="upleft";parent.right.document.keyform.submit();upleftallow=false;upallow=false;leftallow=false;}
if ((downrightallow==true) && (downkeydown==true) && (rightkeydown==true)) {parent.right.document.keyform.direction.value="downright";parent.right.document.keyform.submit();downrightallow=false;downallow=false;rightallow=false;}
if ((downleftallow==true) && (downkeydown==true) && (leftkeydown==true)) {parent.right.document.keyform.direction.value="downleft";parent.right.document.keyform.submit();downleftallow=false;downallow=false;leftallow=false;}
if ((shiftupallow==true) && (upkeydown==true) && (shiftkeydown==true)) {parent.right.document.keyform.direction.value="shiftup";parent.right.document.keyform.submit();shiftupallow=false;upallow=false;}
if ((upallow==true) && (upkeydown==true)) {parent.right.document.keyform.direction.value="up";parent.right.document.keyform.submit();upallow=false;}
if ((downallow==true) && (downkeydown==true)) {parent.right.document.keyform.direction.value="down";parent.right.document.keyform.submit();downallow=false;}
}

function keyup()
{
var keypress=window.event.keyCode;
if (keypress==38) {upkeydown=false;uprightallow=true;upleftallow=true;upallow=true;downallow=true;rightallow=true;leftallow=true;downrightallow=true;shiftupallow=true;downleftallow=true;verify();RegisterKeyUp()}
if (keypress==40) {downkeydown=false;downallow=true;upallow=true;leftallow=true;rightallow=true;downrightallow=true;downleftallow=true;uprightallow=true;upleftallow=true;verify();RegisterKeyUp()}
if (keypress==39) {rightkeydown=false;downrightallow=true;downleftallow=true;uprightallow=true;upleftallow=true;downallow=true;rightallow=true;leftallow=true;upallow=true;verify();RegisterKeyUp()}
if (keypress==37) {leftkeydown=false;downleftallow=true;downrightallow=true;upleftallow=true;uprightallow=true;downallow=true;leftallow=true;rightallow=true;upallow=true;shiftupallow=true;verify();RegisterKeyUp()}
if (keypress==16) {shiftkeydown=false;shiftupallow=true;uprightallow=true;upleftallow=true;upallow=true;downallow=true;rightallow=true;leftallow=true;verify();RegisterKeyUp()}
}

function off()
{
parent.right.document.keyform.direction.value="off";parent.right.document.keyform.submit();
}

function init()
{
document.onkeydown = keydown;
document.onkeyup = keyup;
}


</script>

</HEAD>
<BODY onload = "init()">
<div id="Trace"></div>
</body>
</html>

And here's the PHP that its referring to:

<HTML>
<HEAD>
<body style='overflow:hidden;'>
<form name="keyform" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="direction" value="null">
</form>
<?php

if (isset($_POST['direction']) && $_POST['direction'] == "up") {
system("lptout 253");
system("lptout 253");

}

if (isset($_POST['direction']) && $_POST['direction'] == "shiftup") {
system("lptout 223");
system("lptout 223");

}

if (isset($_POST['direction']) && $_POST['direction'] == "down") {
system("lptout 251");
system("lptout 251");

}

if (isset($_POST['direction']) && $_POST['direction'] == "right") {
system("lptout 254");
system("lptout 254");
}
if (isset($_POST['direction']) && $_POST['direction'] == "upright") {
system("lptout 252");
system("lptout 252");
}
if (isset($_POST['direction']) && $_POST['direction'] == "upleft") {
system("lptout 245");
system("lptout 245");
}if (isset($_POST['direction']) && $_POST['direction'] == "downright") {
system("lptout 250");
system("lptout 250");
}if (isset($_POST['direction']) && $_POST['direction'] == "downleft") {
system("lptout 243");
system("lptout 243");
}
if (isset($_POST['direction']) && $_POST['direction'] == "left") {
system("lptout 247");
system("lptout 247");

}

if (isset($_POST['direction']) && $_POST['direction'] == "off") {
system("lptout 255");
system("lptout 255");

}
?>
</head>
</body></html>

Both of these are called up into frames like this:
<HTML>
<HEAD>
<TITLE>RC</TITLE>
<frameset border="1" cols="2000,*" frameBorder="0" frameSpacing="4">
<noframes>
</noframes>
<frame name="left" src="rc.html">
<frame name="right" src="actions.php" noresize>
</frameset>
</html>

I'm using frames so that when the PHP refreshes, the variables wont refresh because they're in another frame...


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Set focus for JavaScript

JavaScript help! www.computing.net/answers/programming/javascript-help/16308.html

batch file: error SETting in FOR /f www.computing.net/answers/programming/batch-file-error-setting-in-for-f/16520.html

2 questions www.computing.net/answers/programming/2-questions/8761.html