Jump to content

[SOLVED] Javascript Insertions and whitespace


ionik

Recommended Posts

Hello Everyone!

Im having a bit of a problem with white space in Javascript ( and i cannot simply just remove it )

 

The problem I am experiencing is I have created a few various simple functions that insert text into a textarea, the problem I am experiencing is when I try to insert something that contains white space and a line break the code doesn't work giving me a error of a unterminated string. But it works fine if there are no line breaks or white space

 

example works

<img src="/csys/themes/I92/images/icons/quote.gif" onclick="insert_quote('[color=#FF0000][b]Yes[/b][/color] it was crazy beyond believe :drool:', 'i9.ioa^n1k')" border="0" alt="Quote this comment" title="Quote this comment" />

 

Example that doesn't work

<img src="/csys/themes/I92/images/icons/quote.gif" onclick="insert_quote('[quote=i9.ioa^n1k]testing this out@![/quote]

Why doesn\'t this work?', 'i9.ioa^n1k')" border="0" alt="Quote this comment" title="Quote this comment" />

 

the insert_quote function is

function insert_quote(quote, username)
{
surroundText(document.csystem_textarea.message, '[quote=' + username + ']'+quote+'[/quote]');
}

are you using PHP5+ ?

 

if so, you can use json_encode() when printing the data:

 

<?php
  $quote = "[quote=i9.ioa^n1k]testing this out@![/quote]

Why doesn\'t this work?";
  print '<img src="/csys/themes/I92/images/icons/quote.gif" onclick=\'insert_quote('.json_encode($quote).', "i9.ioa^n1k")\' border="0" alt="Quote this comment" title="Quote this comment" />';
?>

are you using PHP5+ ?

 

if so, you can use json_encode() when printing the data:

 

<?php
  $quote = "[quote=i9.ioa^n1k]testing this out@![/quote]

Why doesn\'t this work?";
  print '<img src="/csys/themes/I92/images/icons/quote.gif" onclick=\'insert_quote('.json_encode($quote).', "i9.ioa^n1k")\' border="0" alt="Quote this comment" title="Quote this comment" />';
?>

 

json_encode won't work :)

 

Anyways i solved this problem with a simple modified trim.

 

function deeptrim($quote)
{
$quote = str_replace('
', ' ', $quote);
$quote = trim($quote);
$quote = addslashes($quote);
return $quote;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.