0

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

I have also tried
if (!window.unique_name) { unique_name = true; //code here.. } none of which I can get to work (the global variable is always undefined)
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.

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.
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
(the script in question is a dynamic clock script that grabs the specialised contents of a div and modifies it to something else)

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? (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)

2 Answers 2

1

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:

1) get an instance if the document object and remove the js files (you could do that in a plugin) :

<?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:

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

Sign up to request clarification or add additional context in comments.

5 Comments

thank you for the joomla-specific answer, i will give that a go. as for the 'dirty' solution you suggest, I mentioned that I have indeed tried that, but for whatever reason the window.globalvar is not being set.
may I add, that the script that I am having issues with loading twice is one of my own, not a Joomla script, so it is only present on the page after it gets included there with a PHP require(); snippet
UPDATE: still do not know what or how, but after trying literally everything, including using PHP echo() with a global PHP variable flag being checked & set on every page loading the script... I tried the 'dirty' method one last time and IT WORKS NOW!!! thus accepting your answer :) thanks for your efforts!
@RozzA That's nice! For future reference the js and css files should be included as shown here and here so that you can unset them as described above.
thanks for that extra snippit, it may have been what i was missing
0

If you are having conflicts with PHP require(), you can try require_once(). However, as mentioned, that’s not the Joomla way of doing things.

1 Comment

I have come a long way since this question & have pretty much given up on "the joomla way to do it". require_once can be useful for preventing accidental doubling up on stuff, but in my case does not help at all - a major rethink of code layout solved everything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.