php - 包含 js 库以从正文内部开始,并且仅特定于一页

标签 php structure javascript html-head

我在项目中仅在一个页面中使用谷歌图表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/

相关文章:

javascript - jQuery 插件 : Unexpected behavior - context of "this" is probably lost

javascript - 表达式中的 AngularJS 表达式

php - 为什么 "Strict Standards"不适用于 PHP 构造函数?

c++ - 为什么不止一个元素没有插入一组结构中?

c# - 在 C# 中,我应该使用 Enum、static Class、Dictionary 还是 Struct 来表示这些 "labeled floats"?

arrays - matlab中的结构

javascript - 使用变量显示错误警报,但如果我直接使用值那么它的工作

php - 步骤注册验证以获得更高的转化率

javascript - 带有 Firebase 云消息传递的 Android WebView

代码点火器; $this 在非对象上下文中的用法