views:

44

answers:

3

Hello,

I've created a .php file that write out js code like that:

<?
//my_js.php
// javascript header
header('Content-type: text/javascript');
// Date in the past
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
// always modified
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
// HTTP/1.1
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
?>
//
// js code here
//

then i include the script above in my index.php file like this:

<script type="text/javascript" src="my_js.php?id=<? echo $id ?>">
</script>

This works perfect in Firefox, but SAFARI and CHROME doesn't include my_js.php file at all!

What i'm doing wrong?

**edit:

this is the rendered html in index.php:

<script type="text/javascript" src="my_js.php?id=new"></script> 

and this is the my_js.php code: (it's a very big file so i write out only the first few lines)

var g = { sitepath: "myNullUrl" }

function getBrowserWidth(){
if (window.innerWidth){
    return window.innerWidth;}  
else if (document.documentElement && document.documentElement.clientWidth != 0){
    return document.documentElement.clientWidth;    }
else if (document.body){return document.body.clientWidth;}      
    return 0;
}

that's a strange problem 'cos while i'm viewing source code from Crome/Safari i can access the js file and it seems to be error free!

I'm using Chrome 6.04 and Safari 5, both for mac.

A: 

Maybe set Content-Disposition: inline; header?

        <?php
     if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
      header("Content-type: text/javascript");
      header("Content-Disposition: inline; filename=\"download.js\"");
      header("Content-Length: ".filesize("my-file.js"));
     } else {
      header("Content-type: application/force-download");
      header("Content-Disposition: attachment; filename=\"download.js\"");
      header("Content-Length: ".filesize("my-file.js"));
     }
     header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");
     if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) {
      header("Cache-Control: no-cache");
      header("Pragma: no-cache");
     }
     include("my-file.js

");
?>
oddi
A: 

It may be because it is expecting the file-extension to be my_js.js. If this is the case, save your PHP file as my_js.js, then, assuming you're using Apache, use the Apache Directive: ForceType:

Like so:

<Location /your/path/my_js.js>
      ForceType application/x-httpd-php
</Location>

Good luck!

mkoistinen
Hmm, if he's serving the right content type, this shouldn't be a problem: It would be seriously buggy behaviour on the browser's end to expect a certain extension!
Pekka
I agree, Pekka, but I have some recollection that eariler versions of Safari (in particular) had this issue - and perhaps still does. For example, I often see in the error logs that *.js files work fine, but they throw a warning that they were sent with the wrong header. This would indicate that the browser prefers the extension over the content-type.
mkoistinen
A: 

It should work. Or you can change this:

header('Content-type: text/javascript');

to this

header('Content-type: application/javascript');

Note:

  • application/javascript: JavaScript; Defined in RFC 4329 but not accepted in IE 8 or earlier

  • text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support

andreas