Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ Topic - How do I get a jsp/php variable into client-side javascript? (2013-04-21)

1 view
Skip to first unread message

FAQ server

unread,
Apr 20, 2013, 7:00:00 PM4/20/13
to
-----------------------------------------------------------------------
FAQ Topic - How do I get a jsp/php variable into
client-side javascript?
-----------------------------------------------------------------------

Use a server-side language to generate the javascript.

Certain characters of ECMAScript strings must be escaped by backslash.
These include quote marks, backslash, and line terminators.

JSP Example, using Apache Commons: `org.apache.commons.lang.StringEscapeUtils`:

var jsVar = "<%= StringEscapeUtils.escapeJavaScript(str) %>";

PHP example using `addcslashes`:

var jsVar = "<?php echo addcslashes($str,"\\\'\"\n\r"); ?>";

<URL: http://jibbering.com/faq/example/addcslashes.php>
<URL: http://php.net/manual/en/function.addcslashes.php>
<URL: http://commons.apache.org/lang/>


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

0 new messages