Re: PHP generated JavaScript
| From: | Curt Zirzow | Date: | Mon, 20 Feb 2006 08:19:53 +0000 |
| Subject: | Re: PHP generated JavaScript | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-230733@lists.php.net to get a copy of this message | ||
On Mon, Feb 20, 2006 at 06:29:12PM +1030, Tim Burgan wrote:
> Is is possible to make an external Javascript with PHP.
>
> Am I doing this correcT?
>
> <?php
>
> header("Content-Type: text/javascript");
>
> $text = "Hello World";
>
> echo "alert('".$text."');";
>
> ?>
This looks perfectly fine.
There are a few things you have to be careful with:
- php strings can handle line endings, consider if you have:
$text = "Hello\nWorld";
echo alert('". $text ."');
You will end up with a javscript error.
- A typical javascript file that resides on on a webserver will
only be requested once from the webserver (pending cache
settings) so resources for a page load will be minimal. If you
have a php script output javascript, the browser will request
the file on each request.
Curt.
--
cat .signature: No such file or directory