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]');
}

Link to comment
Share on other sites

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" />';
?>

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.