I’m loading an external .php file using:
<script type="text/javascript" src="myfile.js.php"></script>
Within the external myfile.js.php file, I’m using:
<?php
header('content-type: text/javascript');
$message = "Test message";
?>
document.write('<?php echo $message; ?>');
Everything works fine until I change the name of myfile.js.php to just myfile.php
Why does it stop working if I remove the .js part from the file name? I’m serving the file as text/javascript, plus shouldn’t the .js part be ignored since .php is the actual file extension?
In both cases, the file extension is php, “myfile.js.php” and “myfile.php” – both file extensions are “php”, so that isn’t the problem.
Are you definitely changing the physical file name and the src attribute – or are you just changing the physical file name and forgetting to change the script tag to…
UPDATE: Having Seen The File In Action…
If you look inside of myfile.php, it has a little extra appended to the script, so it starts off well…
But then there is a script block inside of the script file – which is being added to all files…
It is this analytics code that causes the syntax error as these cannot be parsed by the JavaScript engine.