简体   繁体   English

在Joomla中仅包含一次javascript文件

[英]Include a javascript file only once in Joomla

now, this question has been asked and answered successfully many times, yet none of the things i try work. 现在,这个问题已经被问过很多次并成功回答了,但是我尝试的所有事情都没有。

I have tried head.js & require.js libraries 我已经尝试过head.jsrequire.js

I have also tried 我也尝试过
if (!window.unique_name) { unique_name = true; //code here.. } if (!window.unique_name) { unique_name = true; //code here.. } none of which I can get to work (the global variable is always undefined) if (!window.unique_name) { unique_name = true; //code here.. }我都无法工作(全局变量始终未定义)
the script I am trying to include runs something like this: 我尝试包含的脚本运行如下所示的内容:
//clock.js clockyTick = function() { //my code here } setInterval(clockyTick, 1000); the apps that call this script, standalone, work fine. 单独调用此脚本的应用程序可以正常运行。
only when both apps are included on the same page (via calls to PHP require() ) they break. 仅当两个应用程序都包含在同一页面上时(通过对PHP require()调用),它们才会中断。

Here is the cause of the problems (I think): 这是造成问题的原因(我认为):
I am building custom web apps on a (Joomla) site and have the requirement of displaying two of my apps on the same page. 我正在(Joomla)网站上构建自定义Web应用程序,并且要求在同一页面上显示两个应用程序。
Both apps need the same .js file to operate correctly, which works fine when they run standalone, but as soon as both apps are running on the same page (in the admin section) the scripts conflict and stop each other from working 两个应用程序都需要相同的.js文件才能正常运行,这两个应用程序在独立运行时都可以正常运行,但是一旦两个应用程序在同一页面上(在“管理”部分中)运行,脚本就会冲突并导致彼此停止工作
(the script in question is a dynamic clock script that grabs the specialised contents of a div and modifies it to something else) (有问题的脚本是一个动态时钟脚本,它捕获div的专用内容并将其修改为其他内容)

I think the reason I cannot get aforementioned libraries to work, is the fact that they also are being included twice on the admin page. 我认为无法使上述库起作用的原因是,它们在管理页面上也被两次包含。

is there any way around this, or do I have to bite the bullet and integrate a library into the main Joomla template? 什么办法解决这个问题,还是我必须忍耐一下,将库集成到Joomla主模板中? (meaning the library is uselessly loaded on every single page, yet only used on 3 of hundreds) (这意味着该库无用地加载在每个页面上,但仅在数百个中的三个上使用)
jQuery is also required, separately, on each app..but thankfully I am able to use noConflict to avoid problems there (not ideal) 每个应用程序上也分别需要jQuery。但是,值得庆幸的是,我能够使用noConflict避免在那里出现问题(不理想)

The joomla way would be to instantiate the document inside your module and unset only the conflicting script as described in this question here just before you load the module's script: 在Joomla的办法是在这个问题描述实例化模块和取消只是冲突的脚本文件里面在这里你加载模块的脚本之前

1) get an instance if the document object and remove the js files (you could do that in a plugin) : 1)获取文档对象的实例并删除js文件(您可以在插件中执行此操作):

<?php 
         //get the array containing all the script declarations
         $document = JFactory::getDocument(); 
         $headData = $document->getHeadData();
         $scripts = $headData['scripts'];

         //remove your script, i.e. mootools
         unset($scripts['/media/system/js/mootools-core.js']);
         unset($scripts['/media/system/js/mootools-more.js']);
         $headData['scripts'] = $scripts;
         $document->setHeadData($headData);
?>

Or in your case, I think you could try the dirty solution below inside your js files: 或者就您而言,我认为您可以在js文件中尝试以下肮脏的解决方案:

//1st module script

var unique_name;

if (unique_name == false || unique_name == null) { 
    unique_name = true; 
    //code here.. 
    alert("Included 1st script");
}else{
    //do nothing
    alert("Not included 1st script")
}

//2nd module script

var unique_name;

if (unique_name == false || unique_name == null) { 
    unique_name = true; 
    //code here.. 
    alert("Included 2nd script");
}else{
    //do nothing
    alert("Not included 2nd script")
}

Here is a DEMO 这是一个演示

If you are having conflicts with PHP require() , you can try require_once() . 如果您与PHP require()有冲突,可以尝试require_once() However, as mentioned, that's not the Joomla way of doing things. 但是,如上所述,这不是Joomla的处理方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM