Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-14-2012, 11:35 PM   PM User | #1
GreenhornPup
New Coder

 
Join Date: Jun 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
GreenhornPup is an unknown quantity at this point
Unhappy Script to reverse lines won't work

This is absolutely the darnedest thing I have ever seen, and when I think I've seen it all, this takes the cake. OK, I copied an example out of a Beginning JavaScript book - and I mean word for word. The script is supposed to reverse lines in a text area. I am posting two versions of this script: the one I copied out of the book, and a downloaded copy of the same script. Here's the kicker: the downloaded copy works absolutely beautifully, but mine doesn't work at all - and the code is IDENTICAL! Oh, and btw, the browser found no errors in my code. I am posting both of them, and you tell me if there is anything different. Here's mine:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>
function splitAndReverseText(textAreaControl)
{
  var textToSplit = textAreaControl.value;
  var textArray = textToSplit.split('\r\n');

  var numberOfParts = 0;
  numberOfParts = textArray.length;

  var reversedString = "";
  var indexCount;

  for (indexCount = numberOfParts - 1; indexCount >= 0; indexCount--)
  {
    reversedString = reversedString + textArray[indexCount];
    if (indexCount > 0)
    {
      reversedString = reversedString + "\r\n";
    }
  }
  textAreaControl.value = reversedString;
}
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME=form1>
<TEXTAREA ROWS=20 COLS=40 NAME=textarea1 WRAP=soft>Line 1
  Line 2
  Line 3
  Line 4</TEXTAREA>
<BR>
<INPUT TYPE="button" VALUE="Reverse Line Order" NAME=buttonSplit onclick="splitAndReverseText(document.form1.textarea1)">
</FORM>
</BODY>
</HTML>
Now the downloaded copy:

Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JavaScript>
function splitAndReverseText(textAreaControl)
{
   var textToSplit = textAreaControl.value;
   var textArray = textToSplit.split('\r\n');

   var numberOfParts = 0;
   numberOfParts = textArray.length;

   var reversedString = "";
   var indexCount;

   for (indexCount = numberOfParts - 1; indexCount >= 0; indexCount--)
   {
      reversedString = reversedString + textArray[indexCount];
      if (indexCount > 0)
      {
         reversedString = reversedString + "\r\n";
      }
   }

   textAreaControl.value = reversedString;
}
</SCRIPT>
</HEAD>

<BODY>
<FORM NAME=form1>
<TEXTAREA ROWS=20 COLS=40 NAME=textarea1 WRAP=soft>Line 1
Line 2
Line 3
Line 4</TEXTAREA>
<BR>
<INPUT TYPE="button" VALUE="Reverse Line Order" NAME=buttonSplit
   onclick="splitAndReverseText(document.form1.textarea1)">
</FORM>
</BODY>
</HTML>
GreenhornPup is offline   Reply With Quote
Old 08-15-2012, 12:07 AM   PM User | #2
Will Bontrager
Regular Coder

 
Join Date: Jun 2012
Location: Near Chicago, USA
Posts: 123
Thanks: 7
Thanked 19 Times in 19 Posts
Will Bontrager is an unknown quantity at this point
It may be the \r\n at lines 7 and 20.

\r\n is a Windows end of line.

\n is a Unix/Mac OSX end of line.

\r (I think) is a Mac OS 9 and earlier end of line.

When the line ending of the content in the textarea field is different than what the JavaScript is looking for, then the lines won't be split. Without them being split, they can't be reordered.

Try saving both versions with the same line endings. Or, alternatively, replace \r\n with \n on the two lines and see if it will work.

Will
__________________
Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]
Will Bontrager is offline   Reply With Quote
Old 08-15-2012, 12:26 AM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
You could try textToSplit.split( /\r?\n/ );
Logic Ali is offline   Reply With Quote
Old 08-15-2012, 03:17 AM   PM User | #4
GreenhornPup
New Coder

 
Join Date: Jun 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
GreenhornPup is an unknown quantity at this point
That still doesn't explain why the two scripts are identical but one fails. I know this sounds absurd, but it seems like the only difference is the difference in authors, and mine fails because I'm the wrong author, or it hates me, or whatever. Did anyone see any difference b/w the two scripts?
GreenhornPup is offline   Reply With Quote
Old 08-15-2012, 03:58 AM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,797
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
Originally Posted by GreenhornPup View Post
That still doesn't explain why the two scripts are identical but one fails. I know this sounds absurd, but it seems like the only difference is the difference in authors, and mine fails because I'm the wrong author, or it hates me, or whatever. Did anyone see any difference b/w the two scripts?
I copied and pasted both of your posts and neither one worked.

Altered to .split(/\r?\n/) and all works well.
jmrker is offline   Reply With Quote
Old 08-15-2012, 11:44 PM   PM User | #6
Will Bontrager
Regular Coder

 
Join Date: Jun 2012
Location: Near Chicago, USA
Posts: 123
Thanks: 7
Thanked 19 Times in 19 Posts
Will Bontrager is an unknown quantity at this point
Quote:
Originally Posted by GreenhornPup View Post
That still doesn't explain why the two scripts are identical but one fails. I know this sounds absurd, but it seems like the only difference is the difference in authors, and mine fails because I'm the wrong author, or it hates me, or whatever. Did anyone see any difference b/w the two scripts?
Actually, they're not identical. One has 845 characters and the other 864. When two files have identical visible characters, yet behave differently, the invisible characters (spaces, tabs, line endings, etc.) may be the reason for the different behavior.

Will
__________________
Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]
Will Bontrager is offline   Reply With Quote
Reply

Bookmarks

Tags
buttons beginning, functions, novice, regular expressions

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:54 AM.


Advertisement
Log in to turn off these ads.