views:

252

answers:

4

I have the following code which works perfect in IE for a textarea element.

(ed: wrapped for readability)

<textarea name="mem_message" cols="25" rows="5"
  onkeypress="return taLimit(this)" 
  onkeyup="return taCount(this,'myCounter')">
    <? echo $_SESSION['mem_message']; ?>
</textarea>

It calls a validation function:

<script language="Javascript"><!--Counter for Message Box -->

maxL=100;
var bName = navigator.appName;
function taLimit(taObj) {
    if (taObj.value.length==maxL) return false;
    return true;
}

function taCount(taObj,Cnt) { 
    objCnt=createObject(Cnt);
    objVal=taObj.value;
    if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
    if (objCnt) {
     if(bName == "Netscape"){ 
      objCnt.textContent=maxL-objVal.length;}
     else{objCnt.innerText=maxL-objVal.length;}
    }
    return true;
}
function createObject(objId) {
    if (document.getElementById) return document.getElementById(objId);
    else if (document.layers) return eval("document." + objId);
    else if (document.all) return eval("document.all." + objId);
    else return eval("document." + objId);
}
</script>

All the above works on IE only. On Firefox it won't even focus on the box.

A: 

The attributes for event handlers are meant to be in all lower case; have you tried onkeyup rather than onKeyUp, etc.?

T.J. Crowder
Someone seems to have edited the question to make the attributes all lower case now.
T.J. Crowder
These attributes will not be parsed differently by the browser. These are HTML tags, and are not case sensitive. onKeyPress and onKeyUp are html attributes not javascript.
Codex73
It depends what `DOCTYPE` you're using. You're quite correct about the HTML doctypes, though.
T.J. Crowder
+8  A: 

Few pointers:

  • Don't use language="javascript", it's deprecated.
  • Don't use eval for property access, it's slow and unnecessary.
  • Don't sniff for "Netscape", instead check property/method existence/compliance (innerText/textContent)
  • Don't name a method for receiving an element as "createObject", it's misleading.
  • Don't perform undeclared assignment (maxL = 100), it's error prone.
  • Don't capitalize variable names that are not constructors (or namespaces), for convention.
  • Try not to declare functions in global scope, to avoid name conflicts.
kangax
All good points, and nearly any of which might account for things not quite working.
T.J. Crowder
Actually "T.J. Crowder" the problem was solved by implementing "Kangax" first pointer and simply changing the script option to be type="text/javascript". Working properly. If your not helping, please don't trash contributors, learn instead. Thanks "Kangar" !
Codex73
+1  A: 

Here's a better script to count characters in a textarea: http://sliceofcake.wordpress.com/2007/08/16/count-characters-in-a-textarea/

Hope it's what you're looking for!

Emil Stenström
Thanks Emil, no need now. It was a faulty SCRIPT tag attribute.
Codex73
A: 

Please read Unobtrusive JavaScript here http://en.wikipedia.org/wiki/Unobtrusive%5FJavaScript before doing any development in JavaScript.

You will thank me many times over later!

OpenSource