Advertisement
Guest User

Untitled

a guest
May 24th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. // Javascript Cheat Sheet
  2.  
  3. /************************************
  4. *********** DOM: How to... *********
  5. ************************************/
  6. // Find an element by its ID
  7. document.getElementById("anId");
  8.  
  9. // Show an element
  10. element.show();
  11.  
  12. // Hide an element (display:none;)
  13. element.hide();
  14.  
  15. /************************************
  16. ************** Variables ************
  17. ************************************/
  18.  
  19. // Variables: All data has a type associated with it which determines what you can do with it.
  20.  
  21. "I'm a string" // string: a chain of characters
  22. 1 // number: integer
  23. 1.2 // number: float
  24. true // boolean: true or false
  25. ["I'm a string", 1, 1.2, true, false] // array: stores multiple values in a single variable
  26.  
  27. // Set variables
  28. var string = "I'm a string";
  29. var integer = 1;
  30. var float = 1.2;
  31. var boolean = true;
  32. var array = ["I'm a string", 1, 1.2, true];
  33.  
  34. // Change variables
  35. string = "I'm still a string but with another value";
  36. integer = 2;
  37. boolean = false;
  38.  
  39. /************************************
  40. *********** Math expressions *********
  41. ************************************/
  42.  
  43. + // Add
  44. - // Remove
  45. * // Multiply
  46. // Devide
  47. /
  48. % // Modulo: returns the remainder when a division is executed.
  49. 3 % 3 // Returns 0
  50. 3 % 2 // Returns 1
  51. ++ // Adds 1
  52. -- // Removes 1
  53.  
  54. // Comparisons
  55. == // Equal
  56. != // Different
  57. > // Superior
  58. < // Inferior
  59. >= // Superior or equal
  60. <= // Inferior or equal
  61.  
  62. // Conditions
  63. if (someCondition == true) {
  64. // do something
  65. }
  66. else if (someOtherCondition == false) {
  67. // do something
  68. }
  69. else {
  70. // do something
  71. }
  72.  
  73. /************************************
  74. ************** Use Case 1 ***********
  75. ************************************/
  76. var recipe = "Porridge";
  77. var packets = 3;
  78. var isScottish = true; // camelCase when a variable is made of many words.
  79.  
  80. var myBreakfast = "I like having " + recipe + " at breakfast. In fact I eat " + packets + " packets a week.";
  81.  
  82. if (isScottish == true) {
  83. document.getElementById("breakfast").innerHTML = myBreakfast;
  84. }
  85. else {
  86. document.getElementById("breakfast").innerHTML = "I only have coffee.";
  87. }
  88.  
  89.  
  90. /************************************/
  91. /********* Objects: How to... *******/
  92. /************************************/
  93.  
  94. // Get all properties of an object
  95. Object.getOwnPropertyNames(replaceWithYourVariable);
  96.  
  97. /************************************/
  98. /*************** Loops **************/
  99. /************************************/
  100.  
  101. // While loop
  102. var numbers = [1, 2, 3, 4, 10, 20];
  103. var incrementer = 0;
  104. while (incrementer < numbers.length) {
  105. numbers[incrementer] *= 2;
  106. incrementer ++;
  107. }
  108. // returns numbers = [2, 4, 6, 8, 20, 40]
  109.  
  110. // For loop
  111. var numbers = [1, 2, 3, 4, 10, 20];
  112. for (var i = 0; i < numbers.length; i++) { // first we set the value of i, then we set the condition and at the end the incrementent.
  113. numbers[incrementer] *= 2;
  114. }
  115. // returns numbers = [2, 4, 6, 8, 20, 40]
  116.  
  117. /************************************/
  118. /************* Functions ************/
  119. /************************************/
  120.  
  121. function sandwich(filling) { // Define a new function. "filling" is an optional argument. Leave empty parenthesis if the argument is not needed.
  122. alert("A delicious " + filling + " sandwich!");
  123. }
  124. sandwich("extra butter"); // Then use it
  125.  
  126. /************************************/
  127. /************** Events **************/
  128. /************************************/
  129.  
  130. // Click event
  131. selector.onclick = function(){
  132. // Do something
  133. };
  134.  
  135. // Hover event
  136. selector.onmouseover = function(){
  137. // Do something
  138. };
  139.  
  140. /************************************/
  141. /******** Declare Javascript ********/
  142. /************************************/
  143.  
  144. // Inline, in the HTML. NOT RECOMMENDED
  145. <button onclick="style.backgroundColor = 'yellow';">Click me!</button>
  146.  
  147. // At the end of the <body>
  148. <script>
  149. document.getElementById("button").onclick = function(){
  150. this.style.backgroundColor = 'yellow';
  151. }
  152. </script>
  153.  
  154. // In a separated file
  155. <head>
  156. <script type="text/javascript" src="url/to/source"></script>
  157. </head>
  158.  
  159. /************************************/
  160. /************ Libraries *************/
  161. /************************************/
  162.  
  163. // jQuery
  164. // Instead of innerHTML = something;
  165. $( "button.blue" ).html( "I'm feeling blue." ); // Use CSS selectors
  166.  
  167. // Instead of onclick = function(){};
  168. $( "button.clickMe" ).click(function(){
  169. $(this).css("background-color", "blue");
  170. });
  171.  
  172. // Instead of onmouseover
  173. $( "button.hoverMe" ).hover(function(){
  174. $(this).hide();
  175. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement