GeeCON Prague 2014*
The moose likes Mac OS and the fly likes If last char of textarea is \n, Safari 3.2.1 seems to think the textarea contains extra \n. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Mac OS
Bookmark "If last char of textarea is \n, Safari 3.2.1 seems to think the textarea contains extra \n." Watch "If last char of textarea is \n, Safari 3.2.1 seems to think the textarea contains extra \n." New topic
Author

If last char of textarea is \n, Safari 3.2.1 seems to think the textarea contains extra \n.

Louise Brown
Greenhorn

Joined: Sep 08, 2008
Posts: 4
I've written a web page called a.html.

The top part of a.html has a textarea called "userInputArea".

The bottom part echoes the user input. It has a span called "outputNode", which contains the result of JavaScript reading and processing the user input.

Function setResult() copies the content of userInputArea into a string called "userInputString". It then copies userInputString into a string called "outputText". It then calls replaceNodeText(), to replace the contents of outputNode with outputText. (replaceNodeText() is a function in the Head First JavaScript book.)


A friend and I tested entering lines in the textarea, and checking the content of outputNode on the web page. On Windows XP, running Firefox 3.08 and Internet Explorer 8, it ran fine. It also ran fine on Mac OS X 10.5, running Firefox 3.08 and Opera 9.27.

However, I had a problem running Safari 3.21 on Leopard. I put some alerts into a.html, which tell what some counts are. The alerts are triggered by keystrokes. (After you see the alerts, cancel them by clicking on the "OK" button. If you type Enter to cancel the alert, the web page run setResult() becuase you typed a key.)

The problem is that whenever the most recent line that I enter is a blank line, Safari seems to think that there is an extra blank line at the end - a blank line that I didn't enter. Then when I enter a non-blank line, the extra blank line at the end goes away.

For example:

Suppose I type the letter "a". The alerts will say that there is one character in the textarea - the letter "a".

Now I type the "return" key. The alerts will say that I have typed three characters - the letter "a", followed by two carriage returns.

Now I type the letter "b". The alerts will say that I have typed three characters - the letter "a", followed by a carriage return, followed by the letter "b".

If you comment out the alerts so that you're not distracted by them, and the run the test again, you'll see this: After you type a blank line at the end, the "End of the data that you entered" line goes down one line too far, and after you type the "b", the "End of the data that you entered" line moves back up where it should be.

I also tried this on the Safari 4 public beta for Mac OS X. The test worked better with that browser, but it still wasn't right. After I typed a blank line, the "End of the data that you entered" line went down one line too far, and then immediately jumped up to where it should be.

Safari works as I would expect when there are embedded blank lines. I just have problems when the last line is blank.

Am I doing something wrong? Is there a workaround? (This is a simplified version of a web page, which needs the newlines to be correct.)

Thank you very much.
 
GeeCON Prague 2014
 
subject: If last char of textarea is \n, Safari 3.2.1 seems to think the textarea contains extra \n.