{"id":2105,"date":"2023-05-02T18:12:06","date_gmt":"2023-05-02T22:12:06","guid":{"rendered":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/?p=2105"},"modified":"2023-05-02T18:12:12","modified_gmt":"2023-05-02T22:12:12","slug":"jquery-setup","status":"publish","type":"post","link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/topics\/javascript\/jquery-setup\/","title":{"rendered":"jQuery Setup"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Loading jQuery Into a Page<\/h2>\n\n\n\n<p>There are two ways to load jQuery:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Download jQuery and put it in with your site&#8217;s JavaScript files. <\/li>\n\n\n\n<li>Load it from a CDN<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Loading from the CDN<\/h3>\n\n\n\n<p>Most people choose to load it from a CDN. The jQuery CDN allows you to choose which version you want to use.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to the jQuery CDN page: <a rel=\"noreferrer noopener\" href=\"https:\/\/code.jquery.com\/\" target=\"_blank\">https:\/\/code.jquery.com\/<\/a><\/li>\n\n\n\n<li>Choose your version. If you don&#8217;t have a reason to use an older version, then choose the latest minified.\n<ol class=\"wp-block-list\">\n<li>The latest has two minifieds: minified and slim minified. The slim is a smaller filesize but does not include AJAX, animation and effects.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>Copy the script tag that pops up when you click on a version.<\/li>\n\n\n\n<li>Paste the script tag into your HTML right before the closing <\/body> tag.\n<ol class=\"wp-block-list\">\n<li>When using 11ty, this would usually be in the base.njk template so it is available to all pages.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<p>It should look something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"192\" src=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/1879\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM-1024x192.png\" alt=\"screenshot of script tag in HTML page.\" class=\"wp-image-2162\" srcset=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM-1024x192.png 1024w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM-300x56.png 300w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM-768x144.png 768w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM-1536x288.png 1536w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM-1568x294.png 1568w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-28-at-11.54.42-PM.png 1632w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Here is an example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;script\n  src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\"\n  integrity=\"sha256-\/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej\/m4=\"\n  crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n&lt;\/body&gt;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">External JavaScript File<\/h2>\n\n\n\n<p>Most of the time you will write your JS code in its own file. this file should the be included by using a &lt;script&gt; tag that comes right after the previous one to include the jQuery library. Also, it&#8217;s common practice to put your JavaScript files in a <strong>js<\/strong> folder.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"468\" src=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/1879\/2021\/11\/Screen-Shot-2021-11-29-at-12.37.28-AM.png\" alt=\"\" class=\"wp-image-2168\" srcset=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-29-at-12.37.28-AM.png 785w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-29-at-12.37.28-AM-300x179.png 300w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-content\/uploads\/sites\/2916\/2021\/11\/Screen-Shot-2021-11-29-at-12.37.28-AM-768x458.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/figure>\n\n\n\n<p>Here is a code example with both jQuery and the external file (script element for external js file in bold).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  &lt;script\n  src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\"\n  integrity=\"sha256-\/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej\/m4=\"\n  crossorigin=\"anonymous\"&gt;&lt;\/script&gt;\n  <strong>&lt;script src=\"\/js\/main.js\"&gt;&lt;\/script&gt;<\/strong>\n&lt;\/body&gt;<\/code><\/pre>\n\n\n\n<p>It&#8217;s important for your main.js file to be included <strong>AFTER<\/strong> jQuery so it can use jQuery code.<\/p>\n\n\n\n<p>When using 11ty, you usually also put the &lt;script&gt; tag in the base.njk file so it will be included on each page. If you want some JavaScript to just run on one page, then you can create a separate layout file for that page and put the script tags in that file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing Code<\/h2>\n\n\n\n<p>Once jQuery library has been loaded onto the page, you can write jQuery code. Most of the time you want to wait until the page elements have loaded before running your code. There are three different ways you may see to set up jQuery code to run.<\/p>\n\n\n\n<p>All of these would go in the external JavaScript file created above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Classic Version<\/h3>\n\n\n\n<p>How people<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(document).ready(function() {\n  \/\/ do stuff when DOM is ready\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) New Version<\/h3>\n\n\n\n<p>This just uses less code and is the same thing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(function() {\n  <meta charset=\"utf-8\">\/\/ do stuff when DOM is ready\n});<\/code><\/pre>\n\n\n\n<p>We will usually use the classic version in class simply because it&#8217;s easier for beginners to read and know what it&#8217;s doing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3) Minimize impact on other Libraries<\/h3>\n\n\n\n<p>This is NOT the same as the other two. The code in here will run immediately and not wait for the DOM to load. It has a different purpose. It&#8217;s there to prevent jQuery from clashing with another library. jQuery uses the $ dollar sign to start all of its statements. it&#8217;s possible for another library to use these as well so people will use this syntax below to prevent that.  <\/p>\n\n\n\n<p>Technically this is called an <strong>anonymous function.<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(function($) {\n  <meta charset=\"utf-8\">\/\/ do stuff immediately!\n})(jQuery);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Loading jQuery Into a Page There are two ways to load jQuery: Loading from the CDN Most people choose to load it from a CDN. The jQuery CDN allows you to choose which version you want to use. It should look something like this: Here is an example External JavaScript File Most of the time&hellip; <a class=\"more-link\" href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/topics\/javascript\/jquery-setup\/\">Continue reading <span class=\"screen-reader-text\">jQuery Setup<\/span><\/a><\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","portfolio_post_id":0,"portfolio_citation":"","portfolio_annotation":"","openlab_post_visibility":"","footnotes":""},"categories":[20],"tags":[175,176,153],"coauthors":[87],"class_list":["post-2105","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-jquery","tag-load","tag-setup","entry"],"featured_image_urls_v2":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":"","post-thumbnail":"","gform-image-choice-sm":"","gform-image-choice-md":"","gform-image-choice-lg":""},"post_excerpt_stackable_v2":"<p>Loading jQuery Into a Page There are two ways to load jQuery: Download jQuery and put it in with your site&#8217;s JavaScript files. Load it from a CDN Loading from the CDN Most people choose to load it from a CDN. The jQuery CDN allows you to choose which version you want to use. Go to the jQuery CDN page: https:\/\/code.jquery.com\/ Choose your version. If you don&#8217;t have a reason to use an older version, then choose the latest minified. The latest has two minifieds: minified and slim minified. The slim is a smaller filesize but does not include AJAX,&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/category\/topics\/javascript\/\" rel=\"category tag\">JavaScript<\/a>","author_info_v2":{"name":"Christopher Stein","url":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/author\/cstein\/"},"comments_num_v2":"2 comments","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false,"gform-image-choice-sm":false,"gform-image-choice-md":false,"gform-image-choice-lg":false},"uagb_author_info":{"display_name":"Christopher Stein","author_link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/author\/cstein\/"},"uagb_comment_info":2,"uagb_excerpt":"Loading jQuery Into a Page There are two ways to load jQuery: Loading from the CDN Most people choose to load it from a CDN. The jQuery CDN allows you to choose which version you want to use. It should look something like this: Here is an example External JavaScript File Most of the time&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts\/2105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/comments?post=2105"}],"version-history":[{"count":7,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts\/2105\/revisions"}],"predecessor-version":[{"id":2900,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts\/2105\/revisions\/2900"}],"wp:attachment":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/media?parent=2105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/categories?post=2105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/tags?post=2105"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/coauthors?post=2105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}