[JS] How to handle new lines

Started by NanoSector, June 06, 2011, 06:56:42 AM

Previous topic - Next topic

NanoSector

So, this is banging my head to the wall :S

I use a kind of Quick-Edit script for my frontpage.
It works really fine & fast. But... New lines are a BIG problem here.

When I use a new line in my content, like this:
Test

Line test

And then save it, the next time I click the content (to open the textarea to edit it), it refuses to work :S
When I manually remove the new lines in the database, it works again.

I never have been too great in JavaScript :P
Can somebody help me out?

EDIT: Gonna try to use PHP to transform the new lines into _NEW_ and then use PHP to change it to "<br />".
EDIT2: Not working :(
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

kat


NanoSector

Quote from: K@ on June 06, 2011, 07:05:10 AM
Doesn't javascript use "\n"?
Yeah, but replacing those just didn't work (the same result) :S

Oh btw here's my current JS code:
<script type="text/javascript">
function AjaxEditHeader()
{
document.getElementById("indexheader").innerHTML = 'Performing requested operations, this may take some time...';
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// Place our asses against a nice "done" message.
document.getElementById('indexheader').innerHTML = '<form action="http://localhost/map3/index.php?saveheader" name="test?" method="post"><input type="text" name="frontpage_title" value="Testing a title ;)" /><input type="submit" value="Save" /></form>';
}
}
xmlhttp.open("POST","map3.txt",true);
xmlhttp.send();
}
function AjaxEditContent()
{
document.getElementById("indexcontent").innerHTML = 'Performing requested operations, this may take some time...';
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var Contents = "<p>Testing some HTML :)</p>

Test";
// Place our asses against a nice "done" message.
document.getElementById('indexcontent').innerHTML = '<form action="http://localhost/map3/index.php?savecontent" name="test?" method="post"><textarea rows="25" cols="10" name="frontpagecontent">' + Contents + '</textarea><input type="submit" value="Save" /></form>';
}
}
xmlhttp.open("POST","map3.txt",true);
xmlhttp.send();
}
</script>


As you see, I use AJAX for this since normally replacing code with plain JS didn't work :S
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

kat

That was just about my sum knowledge of javascript, I'm afraid... :(

NanoSector

Thanks anyway K@, for actually trying :)

Umm, I pretty much need this solved ASAP :S
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

NanoSector

My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

MrPhil

PHP's nl2br() function? That should turn newlines (carriage returns) into <br>'s. Not sure how you would do it in JS.

You never explained what "refuses to work" really means, so I'm just guessing here.

Arantor

You can't use multi-line JavaScript strings in most browsers, and newlines when injected into <textarea> aren't going to get you anywhere and should be done as <br />'s anyway, e.g.

var Contents = "<p>Testing some HTML :)</p><br /><br />Test";

Moral of the story: convert all newlines into <br /> at the earliest possible opportunity.

NanoSector

Quote from: Once Upon A Star on June 09, 2011, 11:57:13 AM
You can't use multi-line JavaScript strings in most browsers, and newlines when injected into <textarea> aren't going to get you anywhere and should be done as <br />'s anyway, e.g.

var Contents = "<p>Testing some HTML :)</p><br /><br />Test";

Moral of the story: convert all newlines into <br /> at the earliest possible opportunity.
And how do I do that? ::)
Sorry, I don't know a bit of JS here :P
(I was thinking of converting the stuff in the variable and then changing it back in the textarea -- that possible?)

Quote from: MrPhil on June 09, 2011, 11:47:14 AM
PHP's nl2br() function? That should turn newlines (carriage returns) into <br>'s. Not sure how you would do it in JS.

You never explained what "refuses to work" really means, so I'm just guessing here.
It just refuses to perform the JS. It... does not do anything it is supposed to do. Eeh, yeah.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Arantor

Converting the stuff in the variable...

Put whatever's supposed to be in Contents into Contents, then you should be able to use this to perform the switch, but you still can't declare it the way you were trying to (multi line strings in JS are invalid syntactically, small wonder nothing worked)

Contents = Contents.replace(/\n/, '<br />');

NanoSector

Thanks Arantor, I'll try that later after I've re-setup my brand new iTouch.
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

NanoSector

#11
Sorry Arantor, but your method did not work :(

I now use a PHP str_replace() method combined with a simple nl2br().
But even that does not work :S

EDIT:
Can somebody explain how SMF handles this? Maybe I can shake something outta that (while not copying stuff :P).
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Arantor

Hint: look at preparsecode and un_preparsecode. Look specifically at the newline to br conversion.

NanoSector

Quote from: Once Upon A Star on June 11, 2011, 02:18:01 PM
Hint: look at preparsecode and un_preparsecode. Look specifically at the newline to br conversion.
Oh! Thanks. Gonna look into that straight away! :)
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

NanoSector

#14
Not fixed either, however, now it detects numbers als a "\n" :S

E.G. this is my string:
MAP3 1.2 includes a whole new set of features and finally makes use of a database.

It gets transformed into this:
MAP3 <br />.2 includes a whole new set of features and finally makes use of a database.

Am I doing something horribly wrong here? :S
My Mods / Mod Builder - A tool to easily create mods / Blog
"I've heard from a reliable source that the Answer is 42. But, still no word on what the question is."

Advertisement: