Posted By

daisyfig99 on 03/13/13


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

atulkransingh


Testing jQuery and Make it Non-Conflicting


 / Published in: jQuery
 

When you want to test to see if jQuery is working, plus two different examples on how to write jQuery so that it will not conflict with other JavaScript based libraries. Then I show the shorthand way of writing jQuery which I would not get into the habit of. It's not worth the headaches you'll run into when working with a CMS or legacy apps.

  1. <!-- Example 1: Non-Conflicting jQuery -->
  2. jQuery(document).ready(function(){
  3.  
  4. //option 1: add the jqtest ID in your html to the link/a-tag of your choice and then click it for an alert.
  5. jQuery("#jqtest").click( function() {
  6. alert("jQuery is working!");
  7. });
  8.  
  9. //option 2: send a message to the console but the browser must support console.log().
  10. console.log("jQuery is working!");
  11.  
  12. });
  13.  
  14. <!-- ******************************************************* -->
  15.  
  16. <!-- Example 2: Non-Conflicting jQuery -->
  17. jQuery(document).ready(function($) {
  18.  
  19. //option 1: add the jqtest ID in your html to the link/a-tag of your choice and then click it for an alert
  20. $("#jqtest").click( function() {
  21. alert("jQuery is working!");
  22. });
  23.  
  24. //option 2: send a message to the console but the browser must support console.log().
  25. console.log("jQuery is working!");
  26.  
  27. });
  28.  
  29. <!-- ******************************************************* -->
  30.  
  31. <!--
  32. This is the shorthand way to write jQuery although it might conflict with other libraries.
  33. This is just to show the difference.
  34. -->
  35. $(document).ready(function(){
  36.  
  37. $("#jqtest").click( function() {
  38. alert("jQuery might now conflict with other libraries!");
  39. });
  40.  
  41. });

Report this snippet  

You need to login to post a comment.