8

Is it possible to specify the MIME type of a PHP file as application/javascript? I have a PHP file that outputs some JavaScript. When strict MIME type checking is enabled, the browser reports an error:

"Refused to execute script from *.php' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled."

Adding this to the PHP file doesn't help:

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

Here is my code:

<?
  header('Content-Type: application/javascript');
  include ("../admin/config.php");
  include ("../custom_functions.php");
?>
var availableTags = ["BBCD"
<? 
  $prods=decode_prods();
  foreach($prods as $key=>$val){  
?>
, "
<?
  $prods[$key][name] = preg_replace("/(\015\012)|(\015)|(\012)/","",$prods[$key][name]); 
  trim($prods[$key][name]);
  $prods[$key][name]= str_replace("&", "and", $prods[$key][name]);
  $prods[$key][name]= str_replace('"', '', $prods[$key][name]);
  echo $prods[$key][name];
?>
"
<? } ?>
];

I tried adding this to .htaccess after reading this post - but it didn't help.

<Files /js/auto.php>
AddType application/javascript .js
</Files>
5
  • 1
    Show the code please Commented Dec 7, 2016 at 11:40
  • The code displayed is all the code, correct? The reason I ask is you cannot change headers once output has started. If output isn't started before what's displayed here, then what browser are you using and are you sure the header isn't being changed in your included files? Commented Dec 8, 2016 at 11:25
  • @bugfroggy Thats all the code in auto.php. Its called from template file like this: <script type="text/javascript" src="/js/auto.php"></script> Commented Dec 8, 2016 at 11:29
  • That could be your issue. Use application/javascript as text/javascript is deprecated. I don't see why this would cause it but worth a try. Commented Dec 8, 2016 at 11:35
  • @bugfroggy good point - but no that didn't help. Commented Dec 8, 2016 at 12:32

1 Answer 1

7

you mean something like that?

header('Content-Type: application/javascript');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.