In our last article, we showed you how to add external css stylesheets to your Joomla 2.5 content plugin. Adding external javascript files to a plugin can be done as well. While the steps are very similar, there are a few important differences. In this tutorial, we'll show you how to include javascript files with your Joomla 2.5 plugins.

  1. Create the JavaScript File

    The first thing you will need to do is actually create the javascript file. In this example, we have created helloworld.js.

  2. Include the JS file in your XML file

    Your next step will be to update your plugin's XML file to include the new javascript file, helloworld.js. The following code sample highlights the code we added to incorporate helloworld.js

    <files>
        <filename plugin="helloworld">helloworld.php</filename>
        <filename>index.html</filename>
        <filename>mystyle.css</filename>
        <filename>helloworld.js</filename>
    </files>
    
  3. Use document->addScript to load the javascript file

    In our onContentPrepare function, we can use the $document addScript function to add our helloworld.js file:

    public function onContentPrepare($context, &$row, &$params, $page = 0)
    {
        $document = & JFactory::getDocument();
        $document->addScript(JURI::base(). "plugins/content/helloworld/helloworld.js");
    }
    

    Because our javascript file is within our helloworld plugin's folder, we can use JURI::base() to help generate the full URL to our js file.

    After implementing the above steps, Joomla 2.5 adds the following to our header in order to include the file:

    <script src="http://YourDomain.com/plugins/content/helloworld/helloworld.js" type="text/javascript"></script>
    
Like this Article?

Login to comment.

Your Opinion Matters

... but we need to know what you're thinking!

I'm Brad Markle, your friendly Community Support technician, and I wrote the article you're looking at now. I like to think it's perfect, but I'm sure you have some suggestions. Please, let me know what they are!

Feedback
Your Email Address
Because we'd like to talk with you!

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!