Search code examples
joomlamootools

Window.addEvent is not a function after merging js files


well i used joomla's ScriptMerge to merge all js files into one but now i get window.addEvent is not a function on squeeze box initialization,everything else is working fine except for this...i noticed that the squeeze box code is the only javascript block left in the head tag,all the other scripts are merged and moved to body...when i click a button that opens squeeze box it wont open of course,but when i click it again it opens normally...does anyone have an idea how to resolve this error?maybe it is because now the mootools core is being called in the merged js file but not in the head so the squeeze box code returns error because it doesnt have the mootools core?


Solution

  • There is your problem: You are using async attribute on the script - async means the script will load and not holding the dom execution like the default behavior does(without async or defer). so you try to call window.addEvent when you don't have mootools loaded to the page and this is why you get the error of undefined.

    2 solutions for the problem:

    1. remove the async attribute to force the script to hold until loaded and then continue executing the dom.
    2. add to the script tag the onload event and execute the rest of your script there:

    <script src="http://tereni.me/cache/plg_scriptmerge/975e10ecd911c8ca09713d1120c51a6d.js" async type="text/javascript" onload="onLoadScript();"></script>

    and after it inside another script tag:

    function onLoadScript(){
            window.addEvent('domready',function(){
                alert('dom is ready');
            });
        }