我在项目中仅在一个页面中使用谷歌图表js库,并且我有外部和全局页眉和页脚。 head 标签位于 head.php 文件中,并且其中包含所需的 js 库。我的页面的结构是这样的:
<?php
require('header.php');
?>
<div>my body elements of this page lie here </div>
<?php
require('footer.php');
?>
我可以在 header.php 中包含 google 图表库。它将加载到每个页面中,但我只需要在一个页面中加载它。所以,我不想让加载进度变慢。仅当我加载此特定页面时,如何才能包含该库?是否可以?提前致谢。
最佳答案
只需在包含 header 之前添加一个变量,然后在 header.php 中检查该变量
<?php
//page that requires the js
$chartjs = true;
require('header.php');
?>
<div>my body elements of this page lie here </div>
<?php
require('footer.php');
?>
.
<?php
//header.php
if(isset($chartjs) && $chartjs){
//inlcude js file here
}
关于php - 包含 js 库以从正文内部开始,并且仅特定于一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841296/