Using jQuery on Magento 1.9.0.1 rwd Theme: A Comprehensive Guide
Welcome to this comprehensive guide on using jQuery libraries and running custom scripts in the Magento 1.9.0.1 rwd theme, which now includes jQuery by default.
Switching to Google’s CDN for the jQuery Library
By default, Magento uses a locally hosted version of jQuery. However, it is recommended to switch to Google’s Content Delivery Network (CDN) for faster loading times and reduced server load.
-
Open your theme directory and navigate to
skin/frontend/your_package/your_theme/js/jquery.js. -
Comment out or delete the entire contents of this file, as we will no longer use the local library.
-
Add a new file named
jquery.cdn.jsin the same directory with the following content:
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
This script loads Google Analytics for tracking purposes.
- Now, open
skin/frontend/your_package/your_theme/js/app.jsand locate the following line:
$j = jQuery.noConflict();
- Replace it with these lines to include the Google CDN version of jQuery:
var $j = jQuery.noConflict();
$j(document).ready(function() {
$j.getScript('https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', function() {
var $jq = jQuery.noConflict();
// Your code here that uses $jq (stands for jQuery)
$jq(document).ready(function() {
alert("Google CDN jQuery loaded successfully!");
});
});
});
Running Custom jQuery Code
Method 1: Inserting Scripts in PHTML Files
You can insert your custom jQuery code directly into the relevant phtml files, as shown in your example for the minicart.phtml file.
However, ensure you wrap your code inside a $j(document).ready() function to prevent it from executing before the DOM is fully loaded. Here’s the corrected version of your code:
.......
$_cartQty = 0;
}
?>
<script type="text/javascript">
$j(document).ready(function() {
$('#header-cart').hide();
});
</script>
<a href="#header-cart" class="skip-link skip-cart <?php if($_cartQty <= 0): ?> no-count<?php endif; ?>">
<span class="icon"></span>
...........
Method 2: Adding Scripts to app.js
While adding scripts directly in app.js is possible, it’s better to separate your code for better organization and easier maintenance.
-
Create a new file named
my_script.jsinside theskin/frontend/your_package/your_theme/jsdirectory. -
Add your custom jQuery code here:
$j(document).ready(function() {
$('#header-cart').hide();
});
- Now, open
skin/frontend/your_package/your_theme/js/app.jsand add the following lines at the end of the file:
$j(document).ready(function() {
// Other scripts...
});
$j.getScript('js/my_script.js', function() {
var $jq = jQuery.noConflict();
});
This way, your custom script my_script.js will be loaded after the core app.js file.
Conclusion
In this guide, we covered how to switch to Google’s CDN for the jQuery library and how to run custom jQuery code in both phtml files and a separate JavaScript file within the Magento 1.9.0.1 rwd theme.
Remember, it’s essential to use jQuery.noConflict() to avoid conflicts with other JavaScript libraries or native functions. Always ensure your scripts are wrapped inside a document.ready() function for them to execute correctly.
For more in-depth tutorials and resources on Magento development, feel free to explore our Magento Development category at Laravel Company.
Happy coding!