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>
    
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve our Support Center:
Email Address
Optional, but our team may contact you for more information.
Like this Article?

Post a Comment

Name:
Email Address:
Comment:
Are you a bot?
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

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!