I have a problem I would like to compress all my javascript, but inside of my javascript code I use php stuff... like php if, php variables. How can I manage that? Thank you so much
-
Why is there PHP inside your javascript? Is it raw strings?Managu– Managu2009-12-08 14:27:53 +00:00Commented Dec 8, 2009 at 14:27
-
example: I call a different script if a user is logged or not.Nicolo Verrini– Nicolo Verrini2009-12-08 14:39:11 +00:00Commented Dec 8, 2009 at 14:39
4 Answers
To save yourself some trouble, create an object to hold the data your PHP script echo's out. Like so:
var dataPhpEchosOut = {
foo: <?php echo $foo; ?>,
bar: <?php echo $bar; ?>
etc.
};
And use the data object like so:
alert(dataPhpEchosOut.foo + " - " + dataPhpEchosOut.bar);
Now you've got some separation; you can compress the rest of you JavaScript code.
Comments
You could compress the javascript on the fly, using zlib. Most browsers should be able to handle that. However, I don't think it will be worth the extra cpu cycles on the server.
Otherwise, you should do what Mike Atlas says, and partition your dynamic javascript from your static, and only compress the latter.
Comments
If using YUI Compressor: You can trick the compression by using /*!comment blocks surrounding your PHP code: