特定页面中的 Joomla 加载脚本
我正在 Joomla 中创建一个网站,想知道是否有办法使用 php 在特定页面中加载特定脚本....
I'm creating a website in Joomla and wondering if there is a way to load a specific script in a specific page using php....
例如我想加载一个:<script src="sample.js">
在关于我们将要使用的页面中
e.g. i want to load a :
<script src="sample.js">
in the about us page where it is going to be use
我想这样做的原因是,即使页面中没有使用它们,也不会将它们全部加载到头部部分,这会使页面变慢......
the reason why I wanted to do this is that instead of having them all load in head section even if it is not being use in the page makes page slower...
推荐答案
您是否尝试在内容项或组件中执行此操作?如果您尝试在组件中执行此操作,可以使用以下方法将 JavaScript 文件添加到 only 该页面的头部:
Are you trying to do this in a content item or a component? If you're trying to do it in a component, this is the way to add a JavaScript file to the head portion for only that page:
$document =& JFactory::getDocument();
$document->addScript(JURI::base() . 'path/to/sample.js');
如果您尝试在内容项中执行此操作,您很可能需要查找或构建一个内容插件来扫描指示 Joomla 运行上述代码的令牌.我在不久前建立的一个网站上做了这个.我设置了它,以便插件可以在内容项中搜索 {js sample.js} 之类的标签,提取文件名,将脚本添加到文档中,然后搜索并替换文章中的标签.
If you're trying to do this in a content item, you'll most likely need to find or build a content plugin to scan for a token that tells Joomla to run code like above. I did this on one of the sites I built a while back. I had it set so that a plugin would search for tags like {js sample.js} in content items, extract the file name, add the script to the document, then search and replace the tag out of the article.
相关文章