ProblemHelpPlease Posted April 11, 2011 Share Posted April 11, 2011 function addtomo(newcode){var mocontent = document.getElementById("pdata").value;if(/\r\n$/.test(mocontent) || mocontent.length == 0){document.getElementById("pdata").value = mocontent+newcode+"\r\n"}else{document.getElementById("pdata").value = mocontent+"\r\n"+newcode+"\r\n";}} Can anyone see a reson why the above javascript works ok in IE 7 & 8 and only ever returns the 'else' in IE 9 and Firefox. It seems to be the if that is causing the problem. I should be searching the value of the pdata element (which is a textarea) and checking to see if it is empty or if the last charaters are a return \r\nUPDATE: It seems that some browsers use \r\n and others use \n for a return, I dont know why thoughUPDATE 2: If textarea data is passed to either the same page or another page using a form then it passes it with \r\n in any browser, whereas passing it to another page using XML/JS httprequests results in it only passing \n in IE9 and Firefox, and \r\n in IE8 Link to comment Share on other sites More sharing options...
justsomeguy Posted April 11, 2011 Share Posted April 11, 2011 You can check to see if it ends with either \r or \n:if(/\r|\n$/.test(mocontent) Link to comment Share on other sites More sharing options...
ShadowMage Posted April 11, 2011 Share Posted April 11, 2011 You can check to see if it ends with either \r or \n:if(/\r|\n$/.test(mocontent)Wouldn't you want to test for \r\n instead of just \r?Soif (/\r\n|\n$/.test(mocontent)) Link to comment Share on other sites More sharing options...
justsomeguy Posted April 11, 2011 Share Posted April 11, 2011 If it ends with either you want to do the same thing, I assume. Macs will use \r only, because they think different. And just checking if it ends with \n will catch the case where it ends with \r\n, it's redundant to check for both. Link to comment Share on other sites More sharing options...
ShadowMage Posted April 11, 2011 Share Posted April 11, 2011 If it ends with either you want to do the same thing, I assume. Macs will use \r only, because they think different. And just checking if it ends with \n will catch the case where it ends with \r\n, it's redundant to check for both.Oh yea. Good point. I should know by now not to question "Steve". Link to comment Share on other sites More sharing options...
ProblemHelpPlease Posted April 11, 2011 Author Share Posted April 11, 2011 I have fixed it but still dont understnad why it changes between \r\n and \n on the same browser and same PC depending on how the information is passed to another file.I ended up checking for both \r\n and \n as \r on its own means the data would not be in the correct format anyway and would need other alterations making to it. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 11, 2011 Share Posted April 11, 2011 I ended up checking for both \r\n and \n as \r on its own means the data would not be in the correct format anyway and would need other alterations making to it.You don't need to check for \r\n if you're already checking for \n, and Macs use \r as their line endings. Linux typically uses \n, and Windows typically uses \r\n. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.