1

I'm getting this error when I try to call a Javascript function from a Smarty page. I know what the error is about generally - but I don't understand why it happens in this case.

The function which throws the error is called in the following manner:

<a href='#' onClick="showActualSize({$id_product},'{$realsize|escape:'htmlall'}');">Actual Size</a>

In this case, the variables have the following values;

$id_product = 544; $realsize = "Imperial: LR14 Metric: MN1400";

I used escape:'htmlall' to escape any special chars that may appear in $realsize but, as you see, there aren't any in this example.

However, when I look at the page source code, there appear to be new lines in the $realsize variable, as the source looks like this:

<li><a href='#' onClick="showActualSize(544,'Imperial: LR14
Metric: MN1400
');">Actual Size</a></li>

Am I looking at some hidden characters that are causing the error? How can I get around this?

6
  • Escape it as JavaScript, not html, because you're emitting it in JavaScript, not html. Commented Dec 18, 2012 at 10:36
  • @DaveNewton - He actually needs to use both escapes, in sequence. Commented Dec 18, 2012 at 10:37
  • Can you be more specific? I'm very new to Smarty even though I have lots of PHP experience. Commented Dec 18, 2012 at 10:38
  • That'd depend in what the function does, wouldn't it? It only needs to be html-escaped if it shows up in html, which isn't clear. Commented Dec 18, 2012 at 10:40
  • Never mind, using {$realsize|escape:'htmlall'|escape:javascript} fixed the issue. I used html escape because the string could contain quotes etc. Commented Dec 18, 2012 at 10:43

1 Answer 1

1

Using {$realsize|escape:'htmlall'|escape:javascript} has fixed the issue by removing the new line characters which were causing it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.