Re: outputting javascript from a php script?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 10-12-2014 1:13, Jeffry Killen wrote:
One short question:

If I have a javascript script tag in a page that sets the src attribute
to a php script

Before I  have the php script send the response via print  or echo I
have to
call header('content-type: <?>');

so what would be the content type for javascript code?
text/javascript
or
application-x/javascript
or ?

Apologies if this is considered off topic

I have done this with css and used header('content-type text/css')

Also I figured out how to get a self processing page to send a
response to an ajax request sent to it by calling
header('content-type:text/plain')
for a plain text response.

Thank you for time and attention
JK

That's actually quite a difficult question.
What you're basically asking is what is the mime-type of a javascript file.

The answer is that it is officially 'application/javascript'.
However, this has only recently been published as part of the standards. Until just a few years ago, there was no agreed mime-type for javascript files; this is why you'll see text/javascript, application/x-javascript, etc. being used all over the place. The HTML4, html5 and xhtml (1 and 1.1) speccs all expect a text/javascript mimetype when using javascript.

In the end... it doesn't really matter that much. You could technically send a mime-type of text/plain and it would still work just fine. This is simply because most browsers don't trust the headers sent but tend to take a peek into the first packets sent, and decide what it contains based on that.

So... I'd personally use application/javascript since that's the official mime-type. But it doesn't really matter that much which one you use ;)


- Tul

---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux