Here's how I manged to have the code successfully execute on my local box:
js_php.php contents:
<html>
<head><title></title></head>
<body>
<div id="output"></div>
<?php
$myfile = fopen("./text_file.txt", "r");
$pass = fread($myfile,filesize("text_file.txt"));
fclose($myfile);
?>
<script>
var myVar = "<?php echo $pass ?>";
document.getElementById("output").innerHTML = myVar;
console.log(myVar);
</script>
</body>
</html>
text_file.txt contents:
Just another textfile :)
Resulting HTML code for js_php.php:
<html>
<head><title></title></head>
<body>
<div id="output"></div>
<script>
var myVar = "Just another textfile :)";
document.getElementById("output").innerHTML = myVar;
console.log(myVar);
</script>
</body>
</html>
Even though the code works, there are cleaner and faster ways of passing data from PHP to JavaScript. The OP could have passed the data as part of a query string, as follows:
revised js_php.php
<?php
$myfile = fopen("./text_file.txt", "r");
$pass = fread($myfile,filesize("text_file.txt"));
fclose($myfile);
header("location: js_php.html?" . urlencode("qs=$pass"));
exit;
js_php.html:
<html>
<head><title></title></head>
<body>
<div id="output"></div>
<script>
var qs = location.search;
var data = decodeURIComponent( qs );
var split = data.split("=")[1];
split = split.replace(/\+/g," ");
document.getElementById("output").innerHTML = split;
console.log(data);
</script>
</body>
</html>
Note: Since PHP and JavaScript do not encode space characters the same way, the decoded JavaScript will have "+" characters that you need to replace with space characters. To do so, the regex will globally replace all of them in data.
<div><?= $pass; ?></div>? If it actually outputs "<?= $pass; ?>" then your PHP isn't being processed which would be the issue..phtmlor.php