FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How to convert this PHP into JavaScript
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: How to convert this PHP into JavaScript [message #173179 is a reply to message #173176] Sat, 26 March 2011 09:28 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Curtis Dyer wrote:

> sheldonlg wrote:
>
> This is one of those cases in which I feel that cross-posting is
> viable. The problem here is that the OP needs advice on how to
> generate *quality* target code from PHP. Therefore, quality
> JavaScript advice form people who are aware of subtle issues like
> automatic semi-colon insertion (ASI), ECMAScript string literal
> syntax, etc. are necessary.

Generally, full ACK. But I think it is no longer necessary in this case.

>> I just, two days ago, had a need to do just that. What I did
>> was:
>
> I would consider outputting the below outside of PHP tags

Exactly.

> or perhaps using heredoc syntax.

I have found heredoc syntax to be tedious with indentation, and editors
incapable of proper syntax highlighting of non-PHP code in heredoc strings,
so I have begun to avoid it.

>> print "<script>\n";
>> print " var foo = new Array();\n";
>
> Less verbose would be:
>
> var foo = [];
>
>> print " foo[0] = " . $foo[0] . ";\n";
>
> You give no evidence here that `$foo[0]' is properly escaped to be
> included in a string literal

It is inefficiently written, too. Consecutive `echo' or `print' statements
are almost always a sign of cluelessness; in this case, the statements
should have been replaced at first by

?><script>
var foo = [
<?php echo $foo[0]; ?>,
/* etc. (generating the array for javascript) */
];

function myFunc(i)
{
/* do stuff with the foo[i] */
}
</script><?php

Note that the Array literal should not have a trailing comma, for ECMAScript
implementations differ there. The global variable should be avoided in the
first place, of course. And there is still no "javascript":

> There are important differences between PHP's and JavaScript's one needs
> to keep in mind.

One being that JavaScript is only one of many implementations of ECMAScript,
a standard for an extensible programming language.

> In addition to properly escaping quotes and backslashes,
> one needs to take care that the JavaScript string literal has no actual
> line terminators hard coded into it:

Unless preceded by backslash, which has been standardized by ECMAScript
Edition 5, but AFAIK cannot be considered safe to date (thanks to Opera).

> If I haven't missed anything, one way to escape the output for
> JavaScript in PHP might look like:
>
> $escaped = addcslashes($foo[0], "\"\n\r\\'");

You have missed json_encode(), which does that *and* safe encoding of other
Unicode characters, independent of the character encoding of the resulting
document.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: website developers requirements
Next Topic: Aste - PHP template engine
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 19:07:44 GMT 2024

Total time taken to generate the page: 0.04834 seconds