Click to See Complete Forum and Search --> : Is focus() broken in current Netscape/Mozilla browers?


km4hr
03-04-2003, 02:25 PM
Does anyone know for sure if focus() is broken in current Netscape browers. Has anyone actually tested a script that can make the Enter key work like Tab between text fields Netscape 7 form? Can you send it to me?

The following example, and others using focus(), work fine in IE but fail in Netscape. I have an application that requires this function to work properly. Being a strong open source advocate it's embarassing to tell users they have to use IE because Netscape's javascript is apparently brain dead. Ironically Netscape 4 works fine. I'll have to give MS credit for having the best browser in this regard. Strange considering Netscape invented javascript.

http://javascript.internet.com/forms/tab-key-emulation.html

gil davis
03-04-2003, 03:13 PM
Did you lose your other post?

http://forums.webdeveloper.com/showthread.php?s=&threadid=5222

km4hr
03-04-2003, 06:55 PM
Gil,

I read your first response but as you said it was not a solution. I'm not even sure what you intended. You said your solution makes tab work properly. But I'm not having trouble with tab. I want to make enter work like tab. I'll look again at the referenced link but it didn't seem to help much either. That is, I couldn't see how it would cause the enter key to emulate the tab key. I'll look again.

I guess I'm fortunate in that vitually all my users at work have IE. I'm not going to jump through hoops to patch what I consider an error in Netscape. I've been promoting Netscape among my friends because it has some great features for general browsing and because I use Linux personally, which I also recommend. But if Netscape doesn't do javascript correctly then I'm going to keep my mouth shut at work. I'll just check back in another six months to see if things have changed.

Thanks for trying to help.

David

gil davis
03-05-2003, 05:53 AM
Originally posted by km4hr
You said your solution makes tab work properly. But I'm not having trouble with tab. I want to make enter work like tab.I meant that it makes it tab correctly using the enter key. The problem lies in avoiding the default action for the "enter" key in NS, which is to submit the form. You can prove that to yourself by adding

action="blank.htm"

to the FORM tag. When you press enter, you should get a 404 error (unless you have a file called blank.htm in your directory).I'll look again at the referenced link but it didn't seem to help much either. That is, I couldn't see how it would cause the enter key to emulate the tab key. I'll look again.It dosen't. It prevents the "enter" key from submitting the form.I guess I'm fortunate in that vitually all my users at work have IE.Or not. IE will allow you to write crappy code and still function. Netscape will keep to the recommendations and keep you honest. You have a much greater chance of cross-browser success coding for Netscape first.But if Netscape doesn't do javascript correctly then I'm going to keep my mouth shut at work.The JavaScript is working fine. You are trying to make the browser do something against it's design. If you manage to cancel the "submit on enter" feature, you're in great shape.

Here is an explanation of form "rules":

If there is no <input type="submit"> and the only fields in a form are <input type="text">, then the default action for the browser is to submit when the user hits the "enter" key.

Also, it may help you in the future to know that invoking a form's submit() method will not trigger a form's onsubmit handler.

gil davis
03-05-2003, 06:00 AM
Here is the file with fixes to block the enter key submission action.

km4hr
03-05-2003, 08:43 AM
Gil,
Thanks for sticking with me on this. I'm a struggling javascript programmer/wannabe. I've been able to get some useful stuff working but this tab/enter issue has got me stumped, at least in Netscape.

Unfortunately your tab_enter_emulation is still not working for me. It works fine in IE but when I access the the same form using Netscape 7.02 the tab/enter capability fails. I bring up the form and click on box1 to position the cursor. I then press Enter. The cursor momentarily moves to box2 then the form is submitted. Is that not what you're seeing? What browser are you using?

I guess my more general frustration is simply the fact that focus() doesn't seem to work as expected. Being new to this I could be missing something but I've seen posts on other sites indicating others can't get it to work either. I've been looking all over the net for something that works or at least for an answer why not. None of the other experts on this site seems to want to take a shot at either.

Several months ago I even found a Mozilla site where you submit bug reports. Someone else had already made the same submission but now I can't even find the site to check it's status.

Thanks again for going the extra mile and addressing the problem.

David

gil davis
03-05-2003, 09:06 AM
I was testing on NS 6.2. I haven't tried NS 7 yet.

Would I be assuming too much if I asked you if the JavaScript console had anything to say (Tasks|Tools|Javascript Console)?

If you add the 'onsubmit="return false"' to the form tag, does the focus change as expected when you hit "enter"?

If you add 'action="whatever.htm"' to the form tag, do you get a file not found error when you hit "enter"?

Failing all else, go to the news groups

secnews.netscape.com/netscape.devs-javascript

and post your question there.

khalidali63
03-05-2003, 09:48 AM
No way, we don't want a NS loyal to get frustrated only because IE overlooks allots of scripting errors.

here is a modified ( almost new code ) version ,it will work with NS6+ and IE.
NS 4 does not have support for enter button so it will not work with that.


<script type="text/javascript">
nextfield = ""
var shouldSubmit = false;
function setNextField(val){
nextfield = val;
}
function keyUP(e) {
k = (document.all) ? window.event.keyCode : e.which ;
if ((k == 13 || k == 9) && nextfield=="done") { // enter key pressed
shouldSubmit = true;
return true;
} else{
var field = eval('document.form1.' + nextfield)
if(document.all){
field.focus();
}else if(k==13) { // we're not done yet, send focus to next box
field.focus();
}
}
return false;
}

function validateSubmit(){
if(shouldSubmit){
return true;
}
return false;
}
if (!document.all) document.captureEvents(Event.KEYUP);
document.onkeydown = keyUP;
</script>
</HEAD>
<BODY>
<center>
<form name="form1" action="" onsubmit="return validateSubmit();">
Box 1: <input type=text name=box1 onFocus="setNextField('box2');"><br>
Box 2: <input type=text name=box2 onFocus="setNextField('box3');"><br>
Box 3: <input type=text name=box3 onFocus="setNextField('box4');"><br>
Box 4: <input type=text name=box4 onFocus="setNextField('done');"><br>
<input type=submit name=done value="Submit">
</form>
</center>

km4hr
03-05-2003, 10:52 AM
Originally posted by gil davis
I was testing on NS 6.2. I haven't tried NS 7 yet.

Would I be assuming too much if I asked you if the JavaScript console had anything to say (Tasks|Tools|Javascript Console)?
________________________________
Console looks clean.
________________________________


If you add the 'onsubmit="return false"' to the form tag, does the focus change as expected when you hit "enter"?

___________________________________
The original form line already has
'onsubmit="if(!submitted) return false"'. I tried literally what you said and added another 'onsubmit="return=false"'. Nothing changed. I then took out "if(submitted)" from the orginal line and left the rest. Is that what you meant? Anyway, it DOES navigate correctly but submit doesn't work, even when clicking the submit button.
__________________________________

If you add 'action="whatever.htm"' to the form tag, do you get a file not found error when you hit "enter"?
_________________________________
I get file not found error before removing "if(!submitted)" from the original form line. After removal form never submits so I don't get the error.
___________________________________

Failing all else, go to the news groups

___________________________________
Thanks again my friend. You're very patient and helpful. I'll check the news groups.
___________________________________

secnews.netscape.com/netscape.devs-javascript

and post your question there.

pyro
03-05-2003, 10:59 AM
Did you try Khalid's code? It works for me in NN7..

gil davis
03-05-2003, 11:54 AM
The original form line already has 'onsubmit="if(!submitted) return false"'. I tried literally what you said and added another 'onsubmit="return=false"'. Nothing changed. I then took out "if(submitted)" from the orginal line and left the rest. Is that what you meant?I guess I should have been a bit more specific. You can only have one "onsubmit" parameter in the tag at a time. Anyway, it DOES navigate correctly but submit doesn't work, even when clicking the submit button.By "navigate", do you mean "when you hit enter it tabs" action?

km4hr
03-05-2003, 12:44 PM
Originally posted by gil davis
I guess I should have been a bit more specific. You can only have one "onsubmit" parameter in the tag at a time.By "navigate", do you mean "when you hit enter it tabs" action?

__________________________________

Yes, your interpretation of my feeble terminology (navigate) is correct.

_____________________________________

gil davis
03-05-2003, 12:52 PM
So, that should convince you that focus() works in NS 7.

Where do we go from here?

km4hr
03-05-2003, 12:58 PM
Ahah! Khalid's code does seem to have the magic. I'll have to study it a while to see if I can understand how it works and to see how I can apply it to my application. Hmm...my program has a variable number of text fields dependent upon how many records are read from a database. I wonder if there's a another way to refer to the next field than to name each one specifically. Are these objects stored sequentially in an array? But forgive me, I'd don't mean to wear out my welcome on this issue.

I'm grateful to Gil and Khalid for the time and effort. I've learned from both.

gil davis
03-05-2003, 01:11 PM
Are these objects stored sequentially in an array?Yes, they are.

document.formName.elements[...]

But you'd have to weed through them to find the text fields. If you give all the text fields the same NAME it also makes an array:

document.formName.txtBoxName[...]

The array will be in the order that they appear in the HTML code.

km4hr
03-06-2003, 08:27 AM
I wonder how I would go about getting the following obsolete example script replaced by Khalid's improved version?
Maybe the moderator can help.

http://javascript.internet.com/form...-emulation.html


Hello Moderator, are you there?

km4hr
03-06-2003, 08:34 AM
Oops, I just noticed the broken example script is located on someone elses site. Please ignore my last response.
I'm sorry.