2017-10-07 60 views
0

我已閱讀過關於它的文章,但所有解決方案都無法幫助我。 我已將此添加到我的.htaccess文件中:如何在javascript文件中使用php

<FilesMatch "\.(js)$">AddHandlerapplication/x-httpd-php .js</FilesMatch>

這是我的javascript代碼:

$('#calendar').fullCalendar({ 
     header: { 
     left: 'prev,next', 
     center: 'title', 
     right: 'today' 
     }, 
     defaultDate: "<?php echo $current ?>", 
     editable: false, 

但沒有隨後出現。如果有人知道解決方案,我會很感激。

+0

@PatrickEvans [當有隻有一個行是不正確的代碼](http://php.net/manual/en/language.basic-syntax.instruction-separation.php) –

+0

爲什麼你想在這裏使用PHP?只要去'defaultDate:new Date(),'。 – connexo

+0

@PatrickEvans這不是我的整個實際的代碼,它只是它的一部分。 $ current是剛纔定義的。 –

回答

1

在PHP文件中設置$ current爲javascript變量,然後包含這個js文件。 page.php文件

<script type="text/javascript"> 
    var currentDate = <?php echo $current; ?>; 
</script> 

<script src="script.js"></script> 

的script.js

$('#calendar').fullCalendar({ 
     header: { 
     left: 'prev,next', 
     center: 'title', 
     right: 'today' 
     }, 
     defaultDate: currentDate, 
     editable: false 
}); 
+0

這是如何幫助他在'.js'文件中談論php的 –

1

我可以建議用另一種方式生成您的JS代碼。 通過使用PHP文件,例如: myscript.php

// Set the header type to JS 
header('Content-Type: text/javascript'); 

$value = 'Something here'; 

echo "alert('$value');"; 

,然後就包括它使用:

<script src="./myscript.php"></script> 
相關問題