如何将 javascript 添加到 joomla 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6194416/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:49:41  来源:igfitidea点击:

How to add javascript to joomla module?

javascriptjoomlajoomla-extensions

提问by simekadam

Hi I have problem with adding javascript into a joomla module..I've found some solution, but it's not working..

嗨,我在将 javascript 添加到 joomla 模块时遇到问题..我找到了一些解决方案,但它不起作用..

$document = &JFactory::getDocument();
$document->addScript("/career.js");    

These two lines I have in my module, but the script isn't in rendered site.. The file is in root of my web (for test purposes only).

我的模块中有这两行,但脚本不在呈现的站点中。该文件位于我的网站的根目录中(仅用于测试目的)。

Thanks for your help

谢谢你的帮助

采纳答案by simekadam

Ouch..The main problem is, that I am loading the page containing custom module with javascript is loaded via ajax..And all the plugins seems to be adding the js code into head and I only replace some elements into body of my page. exist some solution to this problem?I know I can have all the js logic into one file, but I am not sure it is good way. Is somehow possible to include js directly to module?So that the script tag will be within my document body and correctly included (i am not sure the code will be fired??)

哎哟..主要问题是,我正在加载包含带有javascript的自定义模块的页面是通过ajax加载的..所有插件似乎都将js代码添加到head中,我只将一些元素替换到我的页面正文中。这个问题存在一些解决方案吗?我知道我可以将所有 js 逻辑放在一个文件中,但我不确定这是个好方法。以某种方式可以将 js 直接包含到模块中吗?这样脚本标签将在我的文档正文中并正确包含(我不确定代码是否会被触发??)

Thanks

谢谢

回答by Yesceeohhh

Try this, it's working perfectly.

试试这个,它完美地工作。

$document = &JFactory::getDocument();
$document->addScript('modules'.DS.'MOD_YOURMODULENAME'.DS.'javascript.js');

回答by zod

check the view source .

检查查看源。

copy paste the path of the career.js as browser url and try to access it.

将career.js 的路径复制粘贴为浏览器url 并尝试访问它。

Change the path as needed.

根据需要更改路径。

.   ./   ../

回答by AjayR

You can Use following extension (works both 1.5 and 1.6) to include custom javascript or HTML easily.

您可以使用以下扩展程序(适用于 1.5 和 1.6)来轻松包含自定义 javascript 或 HTML。

http://extensions.joomla.org/extensions/edition/custom-code-in-modules/5435

http://extensions.joomla.org/extensions/edition/custom-code-in-modules/5435

Also when you use directly may use full path like this (change the path of your .JS)

此外,当您直接使用时,可以使用这样的完整路径(更改 .JS 的路径)

<?php
$document = &JFactory::getDocument();
$document->addScript( '/media/system/js/sample.js' );
?>

回答by simekadam

So only workaround which works for me is to add this js to template of my site..But its really awful solution, since I only need it in one article/module

所以唯一对我有用的解决方法是将此 js 添加到我网站的模板中..但它真的很糟糕的解决方案,因为我只在一篇文章/模块中需要它

回答by eyeonu

You could also use the joomla php module http://www.fijiwebdesign.com/products/joomla-php-module.htmlor maybe you found a module that better fits your needs at: http://extensions.joomla.org/extensions/edition/custom-code-in-modules

您还可以使用 joomla php 模块 http://www.fijiwebdesign.com/products/joomla-php-module.html或者您可能在以下位置找到了更适合您需求的模块:http: //extensions.joomla.org/扩展/版本/自定义代码模块

回答by Irfan

Make sure that you have this

确保你有这个

<jdoc:include type="head" />

after head tag in template index.php

在模板 index.php 中的 head 标记之后

回答by Nirav Zaveri

For all those who are landing on this page at a much later date, and looking for solution in Joomla 3.x

对于所有稍后登陆此页面并在 Joomla 3.x 中寻找解决方案的人

You can include a module of type "Custom HTML Advanced" which will NOT filter out any script or style tags. You can use it on any of the pages you want.

您可以包含一个“自定义 HTML 高级”类型的模块,它不会过滤掉任何脚本或样式标签。您可以在任何您想要的页面上使用它。

Hope this helps you :)

希望这对你有帮助:)