Joomla load script in a specific page

2019-03-02 02:24发布

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....

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...

标签: php joomla
1条回答
可以哭但决不认输i
2楼-- · 2019-03-02 03:17

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');

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.

查看更多
登录 后发表回答