Forum Moderators: open

Message Too Old, No Replies

Including php files in javascript

Best practice for php files in js?

         

firebright

8:34 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



What's the best practice for including php in js?

I don't want to use an include, as that would involve rearchitecting the site, aka:

<?php

code to set variable

?>

<script>
alert('<? echo phpvariable;?>');
</script>

Can you include files like so?

<script src="/js/file.php"></script>

It seems to work, but I don't know whether it wil be compatable.

Jonathan

DrDoc

10:34 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, that sure works...
There are just two things you need to make sure:

1) include mime type
<script src="/js/file.php" type="text/javascript"></script>

2) make sure the PHP script returns the correct mime type as well (and not text/html or text/plain)