将 javascript 添加到 Joomla 网站的头部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14903018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding javascript to head of Joomla website
提问by LITguy
I have gone through some Joomla tutorials and I am not understanding how Joomla works. I have never encountered something where every aspect of it evades me. I'm not asking for a free ride.. just where to go or a basic idea of how this works.
我已经阅读了一些 Joomla 教程,但我不明白 Joomla 是如何工作的。我从来没有遇到过它的方方面面都让我望而却步的事情。我不是要求搭便车.. 只是去哪里或这是如何工作的基本概念。
I simply need to add a Panoramio javascript into the <head></head>section of a joomla website. In Word Press I simply download the header.php template and code away.
我只需要将 Panoramio javascript 添加到<head></head>joomla 网站的部分。在 Word Press 中,我只需下载 header.php 模板和代码即可。
It's so confusing understanding Joomla. I do know not to paste directly into an "Article" page so do I have to install some sort of extension or tool to even get this to work?
理解 Joomla 太令人困惑了。我知道不要直接粘贴到“文章”页面中,所以我是否必须安装某种扩展程序或工具才能使其正常工作?
I read to edit the index.php in my templates but I can't even find that. Am I the only person that can't understand Joomla at all? Even the beginner documentation seems to assume I know their system. Thank you in advance.
我阅读以编辑模板中的 index.php,但我什至找不到。我是唯一一个完全不懂 Joomla 的人吗?即使是初学者文档似乎也假设我知道他们的系统。先感谢您。
回答by Lodder
Be careful about which files you add code to. Editing core files like the index.php in the templates folder might not be the best solution. What if there is a template update? The file will get overridden. So just bare that in mind.
请注意向哪些文件添加代码。编辑模板文件夹中的 index.php 等核心文件可能不是最佳解决方案。如果有模板更新怎么办?该文件将被覆盖。所以只要记住这一点。
Before you add the script, it is good idea to get the name of current template:
在添加脚本之前,最好先获取当前模板的名称:
$app = JFactory::getApplication();
$template = $app->getTemplate();
You can use the following to import a .jsfile the <head>tags:
您可以使用以下命令导入.js文件<head>标签:
$doc = JFactory::getDocument(); //only include if not already included
$doc->addScript(JUri::root() . 'templates/' . $template . '/file.js');
or you can add the Javascript there and then like so:
或者你可以在那里添加 Javascript 然后像这样:
$doc = JFactory::getDocument();
$js = "
//javascript goes here
";
$doc->addScriptDeclaration($js);
Hope this helps
希望这可以帮助
回答by David Fritsch
You definitely aren't the only one. Joomla is meant to be able to handle anything you need it to do, and this makes it rather complicated.
你绝对不是唯一的。Joomla 旨在能够处理您需要它做的任何事情,这使它变得相当复杂。
Your best bet is to add the script to the template index.php file that you mentioned if you want it to be available throughout the entire site. If you just needed it for a particular module or component, you would instead want to load it there. In your case, since you are fairly new to Joomla, let's just get it loading!
如果您希望它在整个站点中可用,最好的办法是将脚本添加到您提到的模板 index.php 文件中。如果您只是需要它用于特定模块或组件,您会希望在那里加载它。就您而言,由于您是 Joomla 的新手,让我们加载它吧!
Navigate to your base Joomla directory (where you installed Joomla) and look for the templates folder. Within this you should have several folders. Hopefully you know which template you are currently using and open that folder and there should be an index.php file in it. The top of this file should look similar to the header.php file from Wordpress. Add your script tag to the header element and it should load.
导航到您的基本 Joomla 目录(您安装 Joomla 的位置)并查找模板文件夹。在这里面你应该有几个文件夹。希望您知道当前使用的是哪个模板并打开该文件夹,其中应该有一个 index.php 文件。该文件的顶部应该类似于 Wordpress 中的 header.php 文件。将您的脚本标签添加到标题元素,它应该加载。
If you have no idea what the name of your current template is, go to Extensions->Template Manager on the backend. There should be two with stars to the right of them. One should be marked "administrator" and the other "site". The folder that you are looking for in the templates folder should match the name of the "site" template.
如果您不知道当前模板的名称是什么,请转到后端的扩展->模板管理器。应该有两个星在他们的右边。一个应标记为“管理员”,另一个应标记为“站点”。您在模板文件夹中查找的文件夹应与“站点”模板的名称相匹配。
回答by Valentin Despa
Here is a quick tutorial on what you need to do:
这是有关您需要做什么的快速教程:
- Joomla! templates are at the
/templatesfolder (locate your template) - inside you find the index.php file (there you need to change stuff)
- inside you will find a html page with some PHP inside it
- locate the
<head>tag
- 乔姆拉!模板位于
/templates文件夹中(找到您的模板) - 在里面找到 index.php 文件(在那里你需要改变东西)
- 在里面你会发现一个 html 页面,里面有一些 PHP
- 找到
<head>标签
How add this:
如何添加:
$doc->addScript($this->baseurl.'/templates/'.$this->template.'/javascript/YOURSCRIPT.js', 'text/javascript');
$doc->addScript($this->baseurl.'/templates/'.$this->template.'/javascript/YOURSCRIPT.js', 'text/javascript');
Make sure that this line of code already exists:
确保这行代码已经存在:
$doc = JFactory::getDocument();
$doc = JFactory::getDocument();
Footnote: Because you did not specifiy what Joomla! version you are using, this example is from Joomla! 2.5, the current LTS.
脚注:因为您没有指定 Joomla! 您正在使用的版本,此示例来自 Joomla!2.5、当前的LTS。
P.S. You can also insert the script the 'normal' way, after the <head>tag.
PS 您也可以在<head>标签之后以“正常”方式插入脚本。
回答by Naomi Fridman
you can edit index.php file and other template files as css etc.
您可以将 index.php 文件和其他模板文件编辑为 css 等。
go to : Extensions->template manager
转到:扩展->模板管理器
chose template TAB
选择模板选项卡
Second from the left you see: "template name" Details and Files here you can edit any template file directly on your server.
左起第二个您会看到:“模板名称” 此处的详细信息和文件 您可以直接在服务器上编辑任何模板文件。
Menu can change depending on Joomla version, but that's the general idea.
菜单可能会因 Joomla 版本而异,但这是总体思路。
回答by Peter
I am no expert in JavaScript but I have an AJAX application that works perfectly without modifying any template files. The thing is that it adds the JavaScript at the bottom of the page, not in the head section. I don't know if this is an issue but it works for me.
我不是 JavaScript 专家,但我有一个 AJAX 应用程序,无需修改任何模板文件即可完美运行。问题是它在页面底部添加了 JavaScript,而不是在 head 部分。我不知道这是否是一个问题,但它对我有用。
1) Go to Extensions->Module Manager
1) 转到扩展-> 模块管理器
2) Create a new module. Call it JavaScript Footer (for example)
2) 创建一个新模块。将其称为 JavaScript 页脚(例如)
3) Under the Details tab set Show Title to Hide, set Position to Debug, set Status to Published and set Access to Public
3) 在详细信息选项卡下,将显示标题设置为隐藏,将位置设置为调试,将状态设置为已发布并将访问权限设置为公共
4) Under the Custom Output tab, type <script type="text/javascript" src="http://yourdomain.com/yourscript.js"></script>
4) 在自定义输出选项卡下,输入 <script type="text/javascript" src="http://yourdomain.com/yourscript.js"></script>
I never did understand why some scripts are in the header while others are at the bottom of the body (Google Analytics for example) but it works for me.
我从来不明白为什么有些脚本在标题中,而其他脚本在正文的底部(例如 Google Analytics),但它对我有用。
回答by JMWalker
Joomla is quite different from Wordpress. Actually, index.php is rather useless when customizing joomla template. You will have to explore the blocks of the template in the folder blocks found in your template. For example, to add a scripts in the header section just edit header.php in the directory your_template/blocks.
Joomla 与 Wordpress 完全不同。实际上,在自定义joomla 模板时,index.php 是相当无用的。您必须在模板中找到的文件夹块中探索模板的块。例如,要在标题部分添加脚本,只需编辑 your_template/blocks 目录中的 header.php。

