You can actually insert javascript anywhere in your Joomla article, plugin, module or component. The javascript will run fine.

However, if you want you page to be XHTML compliant, then you must add your javascript to the <HEAD> section of your HTML page.

1. Loading External .js Javascript Files

    Method 1
        If your javascript is all contained in a .js file, you can use the following to load the javascript file. Note: Assume your javascript file is stored in media/system/js/

        JHTML::script('your_javascript_file.js');

        The default location for the javascript file is in media/system/js/. If your javascript file is stored in another folder, you can use the following to load the javascript file:

        JHTML::script('your_javascript_file.js', '/full/path/to/the/js/folder');

        If your javascript requires the requires Mootools, then set the third parameter to TRUE/ to automatically ensure that the Mootools library is loaded.

        // Set 3rd parameter to TRUE to load the Mootools library
        JHTML::script('your_javascript_file.js', '/full/path/to/the/js/folder', true);


    Method 2
        You can also use the following method to load an external javascript file:

        $document = JFactory::getDocument();
     $document->addScriptDeclaration(JURI::base(). 'plugins/content/yourplugin/yourplugin.js');


2. Loading inline Javascript

    Suppose you want to add the following javascript codes to the <HEAD> section of your HTML page:

    <script type="text/javascript">
    window.addEvent('domready', function() {
        new Rokmoomenu($E('ul.menu'), {
            bgiframe: false,
            delay: 500,
            animate: {
                props: ['opacity', 'width', 'height'],
                opts: {
                    duration:400,
                    fps: 100,
                    transition: Fx.Transitions.Quad.easeOut
                }
            }
        });
    });
    </script>


    Use the following to insert the javascript codes:

    $javascript = "
    <script type="text/javascript">
    window.addEvent('domready', function() {
        new Rokmoomenu($E('ul.menu'), {
            bgiframe: false,
            delay: 500,
            animate: {
                props: ['opacity', 'width', 'height'],
                opts: {
                    duration:400,
                    fps: 100,
                    transition: Fx.Transitions.Quad.easeOut
                }
            }
        });
    });
    </script>
    ";

    $document->addScriptDeclaration($javascript);

Best Joomla Hosting Recommendation

ASPHostPortal.com provides our customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. We offer Joomla hosting starts from $5/month only. We also guarantee 30 days’ money back and guarantee 99.9% uptime. If you need a reliable affordable Joomla Hosting, we should be your best choice.