Drupal: .htaccess Access Deny
August 21, 2014 – 7:59 am | No Comment

In this article I will tell how to forbid access to certain resources for some clients. The instructions will include descriptions of different directives.

Read the full story »
CSS Templates

Contain reviews and news about CSS Templates.

Freebies

Contain freebies such as icons, graphics, headers and images for your websites.

Fun Stuff

Contains other fun stuff for entertainment or interesting site showcase.

How-To

Contain technical elaborations on some specific workarounds or common tweak.

Joomla Templates

Contains reviews and news about Joomla templates.

Home » How-To

How to Include and Use JQuery JavaScript Library on Your Joomla-Based Site

Submitted by on November 6, 2009 – 9:09 amNo Comment

This article will tell about a new kind of JavaScript Library – jQuery and describe how you can use it on your Joomla site.

jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.


Get started

To get started, we need a copy of the jQuery library, which we can get from the main download page. The jQuery Starterkit provides some markup and CSS to work with. After downloading and extracting its content we put jquery.js into ..\media\system\js\ folder in the root of your site (all default Joomla labruaries are there). Then open a file you wish to insert code using jQuery JavaScript library.

Calling jQuery

You can call jQuery in one line of the code:

JHTML::_ ( ‘script’, ‘jquery.min.js’ );

If there is another script which is used for a specified component, you can use jQuery in the following way:

$src = JURI::root () . ‘modules/mod_gallery_random/js/';

echo ‘<script type="text/javascript" src="’ . $src . ‘slide.js"></script>';

No-conflict mode

JQuery can work in a special mode (no-conflict). This means that there will no be conflict situations caused by using of this library.

For example, the default Joomla library (MooTools Fx) often acts as a trouble when you use some additional libraries. But this is not bout jQuery!

To create no-conflict mode for jQuery, you should insert the following line into your code:

jQuery.noConflict();

$(‘#id’).val() => jQuery(‘#id’).val()

Working with jQuery

Usually jQuery is included to a web page as one exterior JavaScript-file:

<head>
<script type="text/javascript" src="path/to/jQuery.js"></script>
</head>

You should work with jQuery using $ function. If site contains other JavaScript libraries which use $ as a special symbol, you can use jQuery function (it is the same). This method is preferable.

If you do not wish to create a lengthy code, you can use the following one:

jQuery(function($) {
  // Code of the script where jQuery will be included to $ 
})

Working with jQuery can be divided into two types:

  • Getting of jQuery-object with the help of $() function. For example, passing CSS-selector to $() function, you can get jQuery-object of all HTML elements that are under the criteria and then work with them using different methods of jQuery-object.
  • Calling of global methods of $ object, for example, easy-to-use iterators for array.

An example of handling several DOM nodes is to call $ function with CSS selector string. This returns jQuery object containing some number of elements of HTML-page. The elements are then processed with jQuery methods. For example:

$("div.test").add("p.quote").addClass("blue").slideDown("slow");

It finds all <div> elements of test class and also all <p> elements of quote class and then adds them all blue class and visually slides them down.

Methods beginning from $ are easy-to-used for processing of global objects. For example:

$.each([1,2,3], function() {
  document.write(this + 1);
});

will add 234 to the page.

$.ajax and appropriate functions allow using of AJAX methods. For exaxmple:

$.ajax({
  type: "POST",
  url: "some.php",
  data: {name: 'John', location: 'Boston'},
  success: function(msg){
    alert( "Data Saved: " + msg );
  }
});

In this example, there is a call to some.php script the following parameters: name=John&location=Boston. The received result is issued in a message via alert().

Example of adding click event processor to an element with the help of jQuery:

$(document).ready(function() {
  $("a").click(function() {
    alert("Hello world!");
  });
});

In this case clicking the <A> element causes calling of alert("Hello world!").

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.