避免在页面中多次包含样式表/外部js的最佳方法 [英] Best way to avoid including stylesheets/external js more than once in a page

查看:97
本文介绍了避免在页面中多次包含样式表/外部js的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须清理/维护一些旧的php站点文件.有许多主要的php页面,其中包括基于规则的其他php文件.其中许多包括以下内容的行:

I've having to clean up/maintain some old php site files. There are a number of main php pages which include() other php files based on rules. A number of these includes have lines such as below in them:

<link href="css/styles.css" rel="stylesheet" type="text/css">
<script language="javascript" src="js/functions.js"></script>

在构建php页面之后的某些时候,这些外部文件在同一页面中被多次包含.

Some of the time after the php page has been built, these external files are included more than once in the same page.

这不仅仅是从php include中删除这些外部文件链接的简单情况,因为有时会放置php include,但这是唯一的情况.

It's not just a simple case of removing these external file links from the php includes, as sometimes where the php include is placed, it is the only occurrence.

是否通过php或js进行某种检查,以避免多次出现外部文件?

Is there some kind of checking, either by php or js to avoid multiple occurrences of external files?

谢谢:)

推荐答案

作为一个简单的技巧,请将这些CSS和JS文件添加到PHP文件中,然后将require_once放入PHP文件中.这样,只有在尚未包含文件时,该文件才会被包含.

As an easy hack, add those CSS and JS files into a PHP file, and put a require_once to the PHP file. That way the file will only be included if it hasn't been included yet.

这篇关于避免在页面中多次包含样式表/外部js的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆