Sastrija
Sastrija

Reputation: 3384

Placing jquery plugin in joomla module

I'm trying to create a module in joomla, which uses one jquery plugin. I've to do an ajax operation while clicking on an element in the module. Currently I'm specifying the whole path to the php file. But I know Its a wrong method.

The code in jquery plugin is like this.(please note the second line where the path is specified in the jquery plugin file)

       $.get(
            "/subdirectory/MyBlog/modules/mod_calendar/getCalendar.php", {
                month: $("#selectedMonth").val(),
                year: $("#selectedYear").val(),
                flag: '-1'
            }, function(data){
                $("#monthlyCalendar").html(data);
                $("#monthlyCalendar").show();
            }
        ); 

What is the correct method to specify the path in a jquery plugin file. Also I need to know where to put my jquery plugin file in the module.

Upvotes: 1

Views: 1487

Answers (3)

Sastrija
Sastrija

Reputation: 3384

Atlast I was able to find a good solution to use the Ajax using Jquery in Joomla.

For this first you need to create a view and model to get required html through AJAX call.

Then use jQuery code similar to the following to get only the output of the required view.

//Code to get the base URL of joomla installation
szURL = document.URL;
componentList = szURL.split('/');
szDocument = componentList[componentList.length-1];
szURL = szURL.replace(szDocument, "");

//URL to the required component
url = szURL + "?option=COMPONENT_NAME&view=VIEW_NAME&tmpl=component&uid=" + getRandomValue();
jQuery.get(url, function(data) {
    jQuery("#mydiv").html(data);
});


//Function to get a random number
//It is used for Ajax calls from IE; Else IE will use the cache values
function getRandomValue(){
    return Math.floor(1000000 * (Math.random() % 1))
}

Note the URL used for the ajax call. It uses "tmpl=component" to get only the html for the selected component without joomla HTMLs.

Upvotes: 0

Sastrija
Sastrija

Reputation: 3384

I found an answer in the following blog.

http://blog.subooa.com/development/joomla-coding/ajax-in-joomla-with-jquery/

Upvotes: 1

Will Mavis
Will Mavis

Reputation: 393

The best way I found to do it is to use the JURI::root method to create a javascript variable that I can then use. In your php code, you would do something like this:

?>

<script type="text/javascript">
        var joomlaRoot = '<?php echo JURI::root(); ?>';
</script>

<?php

You can then use that variable when you are doing your AJAX call.

As for where to put the jquery plugin file in your module, you can put it whereever you want under your module's directory and then use JURI::root again to create a path to it and call the JDocument::addScript method.

On a side note, you may consider using MooTools. It comes bundled in Joomla! already. It has the ability to do AJAX calls. Also, by using it, you avoid the possibility of having jQuery conflicts.

Upvotes: 0

Related Questions