如何让外部.js文件在joomla中工作

How to get external .JS files to work in joomla

本文关键字:joomla 工作 文件 js 外部      更新时间:2023-09-26

我正在创建一个新的Joomla模板,并且遇到了链接到外部.js文件的问题。

    <head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/main.css">
<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/FLpresentation.css">
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/jquery-2.1.4.js"></script>
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/FLpresentation.js"></script>
<script src="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/js/FLajax.js"></script>

上面的代码包含在head标签中,当我将模板安装到joomla中时,CSS工作正常。一切看起来应该如何,但我所有的Jquery和Javascript代码不工作。唯一这样做的代码是在index.php文件的标记内。

谁能告诉我我做错了什么?我已经在templateDetails.xml中定义了文件和文件夹,除了js之外的一切都工作得很好,尽管我使用了完全相同的方法来链接CSS。

100% JS冲突。检查一下控制台!虽然包含了Jquery库,但Joomla已经自带了Jquery。检查控制台是否有错误,检查Jquery在输出HTML中是否超过1次,并迁移代码。

也像这样包装你的代码:

(function($) {
    $(document).ready(function(){
        // START HERE FOR ON_DOCUMENT_READY
        alert(1);
    });

})(jQuery);

使用JHtml::script方法,如下例所示:

$url = Juri::base();// pick the url for my joomla website
JHtml::script($url . 'components/com_revista/prefixfree.min.js');//here i'm using one file in my server, but you could put the url for your script like 
"JHtml::script('http://urlforscriptsource/script.js');"

下面的代码为您的joomla扩展添加JQuery框架:

JHtml::_('jquery.framework');
JHtml::_('jquery.ui');
JHtml::_('jquery.ui', array('sortable'))

;

Ps:在你的JQuery代码中最好使用JQuery()而不是$(),这将避免冲突!