473,406 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Keypress irregularity: IE vs. FF function call timing?

Hello,

I am programming a little game, and I want the focus to move from one
input box to the next as the user types. I have image-input pairs so
the user is typing a letter under a picture. (Similar to word jumbles
or cryptograms.) I'm also keeping track of the user's complete guess
in a cookie so they can save their game. I get the input; if it's a
letter, I capitalize it and update the cookie; then move the focus to
the next input box. It works as I expect in FF, but not in IE.

In IE, it just capitalizes the letter, but won't move the focus. Now,
if I comment out the cookie handling section or type a number, it will
update the current input box, move to the next, and put in the user's
input (numbers, lowercase letters, etc) there too. So something must
be happening in the cookie handling section, although it works as
expected and the JavaScript Console doesn't display any errors or
warnings.

I'm wondering if the JavaScript run-time is somehow not finishing
everything before it calls the last line, MoveFocus(). BTW, I've tried
putting MoveFocus() in the onchange= attribute but it was never
triggered. I guess keypress + updating the value doesn't count as a
change.

Thanks for any hints, debugging, or insight you can provide.

HTML:
<div class="pair">
<img id="q1" src="img.jpg" alt="image" class="black" />
<br />

<input id="u1" type="text" class="guess" size="1" maxlength="1"
value=""
onFocus="Highlight(this);" onBlur="UnHighlight(this);"
onKeyPress="ReplaceVal(event);" />
</div>

javascript:
function MoveFocus( objid )
{
var id = objid.slice( 1 );
id++;
document.getElementById( "u"+id ).focus();
return true;
}

function ReplaceVal(e)
{
// Update interface
var keynum;
var numcheck;
var obj;

// Get the value of the user's input
if(window.event) // IE
{
keynum = e.keyCode;
obj = e.srcElement;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
obj = e.target;
}
var keychar = String.fromCharCode(keynum)

// Ignore numbers and punctuation
recheck = /[a-zA-Z]/;
if( !recheck.test( keychar) )
{ obj.value = ' '; }
else
{
obj.value = keychar.toUpperCase();

// Find the guess cookie
var oldguess;
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++)
{
var c = ca[i];
var pair = c.split('=');
if( pair[0] == 'guess' )
{
oldguess = pair[1];
break;
}
}

// Update the cookie
var lind = obj.id.slice(1);
var newguess = oldguess.slice( 0, lind) + obj.value +
oldguess.slice( lind+1 );
document.cookie = 'guess='+newguess+';';
}

MoveFocus( obj.id );
return true;
}

Jun 17 '06 #1
1 2027
Well, there were errors in the cookie section. I was thinking PHP and
forgot the second argument for slice. Fixing that error was one of many
iterations. Working Javascript function follows:
function ReplaceVal(e)
{
var keynum;
var numcheck;
var obj;

// Get the value of the user's input
if(window.event) // IE
{
keynum = e.keyCode;
obj = e.srcElement;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
obj = e.target;
}
var keychar = String.fromCharCode(keynum)

// Ignore numbers and punctuation
recheck = /[a-zA-Z]/;
if( !recheck.test(keychar) )
{
obj.value = '';
}
else
{
obj.value = keychar.toUpperCase();

// Update the cookie
var oldguess = getCookie( "guess" );

if( oldguess )
{
var lind = obj.id.slice(1);
var preslice = oldguess.substring(0,lind);
var postslice = oldguess.substring( ++lind, oldguess.length );

var newguess = preslice+obj.value+postslice;
setCookie("guess", newguess );
}
}

MoveFocus( obj.id );
}

Jun 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
3
by: Hamish | last post by:
I am trying to detect the up and down keys on a key press function, but they do not seem to generate a keypress call. Is there another event generated when these keys are pressed Thanks. PS...
11
by: srkkreddy | last post by:
Hi, I have written a large program which makes multiple calls to number of functions (also written by me) of the program. Now, I want to know the collective time taken by all the calls to a...
3
by: Raul M. Colon | last post by:
Is possible to assign a click event to a button control in a Web form just pressing the return key? Something like windows forms where you can assign this action to a default control. For example,...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
11
by: The Frog | last post by:
Hi all, Maybe I am just missing something simple here, but I seem to have an issue with a callback function in A97 that is used to fill a Listbox with values. The first time the callback...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.