var textarea; onload=function(){ textarea=document.getElementsByTagName('textarea')[0]; } function getNewlinePos_back(text,pos){ while(pos>=0 && text.charAt(pos)!='\n') pos--; return pos; } function getNewlinePos_forth(text,pos){ let len = text.length; while(pos