4

I am facing some javascript issues by loading order of custom module Javascript files, so i want to change the loading order of my Javascript files located at magento root root/js/.

But the Javascript files are added by Helper as i mentioned below.

 public function addAssets(Mage_Core_Model_Layout $layout)
    {
        $head = $layout->getBlock('head');
        $content = $layout->getBlock('content');

            $head->addCss('webforms/form.css');
            $head->addJs('prototype/window.js');
            $head->addItem('js_css', 'prototype/windows/themes/default.css');
            $head->addItem('js_css', 'prototype/windows/themes/alphacube.css');
            // logic
            $head->addJs('webforms/logic.js');

            // multistep
            $head->addJs('webforms/multistep.js');

            // stars
            $head->addJs('webforms/stars.js');
            $head->addCss('webforms/stars.css');

            // tooltips
            $head->addJs('webforms/HelpBalloon.js');
            $head->addCss('webforms/helpballoon.css');

            // wysiwyg
            $head->addJs('tiny_mce/tiny_mce.js');

            // calendar
            $head->addJs('calendar/calendar.js');
            $head->addJs('calendar/calendar-setup.js');
            $head->addItem('js_css', 'calendar/calendar-blue.css');

        }

I want to load above jsvascript files before js/varien/product.js.

Can anyone tell me how to change the sort order or loading order of the Javascript files.

0
2

To change the order in which JavaScript (or CSS) files are added to the Magento code, you will have to remove the other files first. Magento will just add new JavaScript files to the end of the existing list of files.

To place your script just before the js/varien/product.js file, you should remove that file first. After that, you should add your file first and then the original file you just removed. This would look something like this:

$head->removeItem('js', 'js/varien/product.js');
$head->addItem('js', 'path/to/mycustom.js');
$head->addJs('js/varien/product.js');

This should answer your question. However, this might result in other issues as well, because of some dependencies (didn't test this though).

If possible, it would be better to make some changes to your JavaScript file, so it will not rely on the order in which the JavaScripts are added. I don't know the code you want to add, so it's hard to tell if and how this will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.