<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaScript Archives - Sourced Code</title>
	<atom:link href="https://sourcedcode.com/blog/category/javascript/feed" rel="self" type="application/rss+xml" />
	<link>https://sourcedcode.com</link>
	<description>AEM Blog Made Just for You.</description>
	<lastBuildDate>Fri, 08 Dec 2023 03:23:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.8</generator>

<image>
	<url>https://sourcedcode.com/wp-content/uploads/2019/09/cropped-favicon-32x32.png</url>
	<title>JavaScript Archives - Sourced Code</title>
	<link>https://sourcedcode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Execute JavaScript in the Author Environment for AEM Components AEM 6.5+</title>
		<link>https://sourcedcode.com/blog/aem/execute-javascript-in-the-author-environment-for-aem-components-aem-6-5</link>
					<comments>https://sourcedcode.com/blog/aem/execute-javascript-in-the-author-environment-for-aem-components-aem-6-5#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 08 Dec 2023 00:47:15 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6322</guid>

					<description><![CDATA[<p>To execute JavaScript specifically for the AEM Author Environment, developers can add a condition to check for a Cookie stored in the browser, &#8216;cq-authoring-mode&#8217;; this works on AEM 6.5+. When the Cookie of &#8216;cq-authoring-mode&#8217; exists, then execute JavaScript during authoring on the editor.html. Only in the Author Environment, the Cookie, &#8216;cq-authoring-mode&#8217;, will exist. Below is [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/execute-javascript-in-the-author-environment-for-aem-components-aem-6-5">Execute JavaScript in the Author Environment for AEM Components AEM 6.5+</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>To execute JavaScript specifically for the AEM <strong>Author Environment</strong>, developers can add a condition to check for a Cookie stored in the browser, &#8216;cq-authoring-mode&#8217;; this works on AEM 6.5+. When the Cookie of <strong class="code-highlight-primary">&#8216;cq-authoring-mode&#8217;</strong> exists, then execute JavaScript during authoring on the editor.html. Only in the Author Environment, the Cookie, <strong class="code-highlight-primary">&#8216;cq-authoring-mode&#8217;</strong>, will exist. Below is a code snippet demonstrating how to check for the existence of the <strong class="code-highlight-primary">&#8216;cq-authoring-mode&#8217;</strong> key in the cookies and gracefully exit the JavaScript logic if the key is found (targeting only the Author Environment, which means run-mode=author):</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #006600; font-style: italic;">// Import the necessary modules</span><br />
<span style="color: #FF0000;">import</span> Cookies from <span style="color: #3366CC;">'js-cookie'</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Check if the 'cq-authoring-mode' key exists in cookies</span><br />
<span style="color: #000066; font-weight: bold;">const</span> authoringModeCookie <span style="color: #339933;">=</span> Cookies.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cq-authoring-mode'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>authoringModeCookie<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Exit the JavaScript logic if the key is found</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Authoring mode cookie found. Exiting JavaScript logic.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Continue with your JavaScript logic for AEM Author Environment</span><br />
&nbsp; console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Authoring mode cookie not found. Proceeding with JavaScript logic.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #006600; font-style: italic;">// Add your custom functionality here</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>In this example, we use the &#8216;js-cookie&#8217; library to handle cookies in a simplified manner. The Cookies.get(<strong class="code-highlight-primary">&#8216;cq-authoring-mode&#8217;</strong>) function retrieves the value of the <strong class="code-highlight-primary">&#8216;cq-authoring-mode&#8217;</strong> key from cookies. If the key exists, the JavaScript logic exits gracefully. Otherwise, you can proceed with implementing your custom functionality.</p>
<hr class="spacer-large">
<h2>Benefits of Customized JavaScript in AEM Editor</h2>
<ol>
<li><strong>Context-Aware Functionality: </strong>By checking for specific conditions, such as the presence of a key in cookies, developers can implement context-aware functionality tailored to the AEM authoring environment.</li>
<li><strong>Enhanced Authoring Experience: </strong>Custom JavaScript allows for the augmentation of the authoring experience by introducing features that streamline workflows and improve usability for content authors.</li>
<li><strong>Graceful Handling of Conditions:</strong> Checking for the existence of keys in cookies enables developers to gracefully handle conditions, ensuring that the JavaScript logic behaves appropriately in different scenarios.</li>
</ol>
<hr class="spacer-large">
<h2>Conclusion</h2>
<p>Mastering the art of writing JavaScript for the AEM Author Environment empowers developers to create a tailored experience purely for the authoring environment. By incorporating checks for specific conditions, such as cookie existence, developers can introduce intelligent and context-aware functionalities that enhance the content creation process. The provided code snippet serves as a starting point for customizing JavaScript in the AEM Author Environment, offering a foundation for further innovation and optimization.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/execute-javascript-in-the-author-environment-for-aem-components-aem-6-5">Execute JavaScript in the Author Environment for AEM Components AEM 6.5+</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/execute-javascript-in-the-author-environment-for-aem-components-aem-6-5/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Webpack Dev Server ^3.9.0 unable to POST (send data to server)</title>
		<link>https://sourcedcode.com/blog/javascript/webpack-dev-server-3-9-0-unable-to-post-send-data-to-server</link>
					<comments>https://sourcedcode.com/blog/javascript/webpack-dev-server-3-9-0-unable-to-post-send-data-to-server#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 28 Oct 2022 21:36:50 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3855</guid>

					<description><![CDATA[<p>While working on the web pack dev server (version ^3.9.0), you may have changed some configurations with the proxy configuration, so you can proxy specific paths like /api or /bin to refer to a server of your choice; in this example, we are referencing to our AEM local environment in localhost:4502. While making server calls, [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/javascript/webpack-dev-server-3-9-0-unable-to-post-send-data-to-server">Webpack Dev Server ^3.9.0 unable to POST (send data to server)</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>While working on the web pack dev server (version ^3.9.0), you may have changed some configurations with the proxy configuration, so you can proxy specific paths like /api or /bin to refer to a server of your choice; in this example, we are referencing to our AEM local environment in localhost:4502. While making server calls, the <strong>GET</strong> HTTP Methods are working as expected and proxying local AEM.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">devServer<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; port<span style="color: #339933;">:</span> <span style="color: #CC0000;">9999</span><span style="color: #339933;">,</span><br />
&nbsp; inline<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; proxy<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; context<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'/bin'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; target<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://localhost:4502'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; writeToDisk<span style="color: #339933;">,</span><br />
&nbsp; liveReload<span style="color: #339933;">:</span> <span style="color: #339933;">!</span>writeToDisk<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>However, the <strong>POST</strong> HTTP method does not work. You are likely to see a <span style="color: red;">403 Forbidden error</span>; unable to send data to local AEM (proxy server). A simple solution to fix this is to add append another configuration property to your dev server key named <strong>setup()</strong>. This will take the original POST URL and redirect with the same URL, making it a GET request; it&#8217;s a bit hacky, but it works well.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">devServer<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; port<span style="color: #339933;">:</span> <span style="color: #CC0000;">9999</span><span style="color: #339933;">,</span><br />
&nbsp; inline<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; proxy<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; context<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'/bin'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; target<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://localhost:4502'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; setup<span style="color: #009900;">&#40;</span>app<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; app.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'*'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; res.<span style="color: #660066;">redirect</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">originalUrl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; writeToDisk<span style="color: #339933;">,</span><br />
&nbsp; liveReload<span style="color: #339933;">:</span> <span style="color: #339933;">!</span>writeToDisk<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<p>This article should answer your questions for:</p>
<ul>
<li>Webpack Dev Server, unable to make a POST to the proxied server?</li>
<li>Webpack Dev Server, proxy configurations does not allow me to POST?</li>
<li>Webpack Dev Server, I am getting a 403 Forbidden error whenever I try to make a POST via proxy configurations?</li>
<li>Webpack Dev Server, Why can&#8217;t I make a POST request to my proxy backend server?</li>
<li>Webpack Dev Server, Why can&#8217;t I send data to to my proxied server?</li>
<li>Webpack Dev Server, 301 forbidden POST request, not able to send data to my server?</li>
<li>How do I get Webpack Dev Server to accept POST requests</li>
</ul>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/javascript/webpack-dev-server-3-9-0-unable-to-post-send-data-to-server">Webpack Dev Server ^3.9.0 unable to POST (send data to server)</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/javascript/webpack-dev-server-3-9-0-unable-to-post-send-data-to-server/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Race Conditions with window.Granite?.I18n</title>
		<link>https://sourcedcode.com/blog/aem/race-conditions-with-window-granite-i18n</link>
					<comments>https://sourcedcode.com/blog/aem/race-conditions-with-window-granite-i18n#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 21 Sep 2022 01:17:24 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3815</guid>

					<description><![CDATA[<p>We have created a JavaScript micro-frontend in VueJS to handle a new feature for our website. The JavaScript micro-frontend is set up nicely inside of a maven module. Along with it&#8217;s own Node Package Manager configuration, we used the aem-clientlib-generator to generate a client library under /apps/sourcedcode/clientlibs/micro-frontend. Within our VueJS application, we have a utility [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/race-conditions-with-window-granite-i18n">Race Conditions with window.Granite?.I18n</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We have created a JavaScript micro-frontend in VueJS to handle a new feature for our website. The JavaScript micro-frontend is set up nicely inside of a maven module. Along with it&#8217;s own Node Package Manager configuration, we used the <a href="https://www.npmjs.com/package/aem-clientlib-generator" rel="noopener" target="_blank">aem-clientlib-generator</a> to generate a client library under /apps/sourcedcode/clientlibs/micro-frontend. </p>
<p>Within our VueJS application, we have a utility named i18n.js, the file looks like this (code below). Throughout application, we will call this util i18n(function) to get values from our i18n AEM dictionary.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #FF0000;">export</span> <span style="color: #000066; font-weight: bold;">default</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; i18n<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> notes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">Granite</span><span style="color: #339933;">?</span>.<span style="color: #660066;">I18n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> window.<span style="color: #660066;">Granite</span>.<span style="color: #660066;">I18n</span>.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> notes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> id<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>When we test out the micro-frontend, we noticed that sometimes the window.Granite, AEM global object, is intermittently working; sometimes the window object is valid/not, so dictionary values are loaded, and sometimes they are not. Why window.Granite is sometimes undefined? Well, this looks like a race condition issue.</strong></p>
<div class="mention-block">
<strong>What is a race condition?</strong><br />
In short, a race condition occurs when a device or system attempts to do two or more actions at the same time, but the actions must be performed in the proper sequence to be performed correctly.<br />
</strong>
</div>
<p>How can we ensure window.Granite is loaded first before our application is loaded next? In the next section let&#8217;s look at the different ways to see how we can do this.</p>
<hr/>
<p><strong>Solutions</strong></p>
<ol>
<li><a href="#manually">Manually Invoking the i18n.js from /libs/clientlibs/granite/utils/source/I18n.js</a></li>
<li><a href="http://via-clientlibrary">Include the granite.utils as a dependency</a></li>
</ol>
<hr class="spacer-large" />
<div style="text-align:center; margin-bottom: 100px;">
<h2>Solutions</h2>
<p>In this section of the blog, you will find two solutions to this problem.
</p></div>
<h3 id="manually">1. Manually Invoking the i18n.js from /libs/clientlibs/granite/utils/source/I18n.js</h3>
<p>From the page template that your micro-frontend is embedded, you can just call the Adobe core Granite utils > i18n.js before your custom code, like the code provided below. Viewing the source code of your HTML page should look something like this; where the i18n.js is loaded first, and then your application is loaded secondly. Using the proxy method for accessing client libraries, you can serve /libs/clientlibs/granite/utils/source/I18n.js with /etc.clientlibs/clientlibs/granite/utils.js.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;/etc.clientlibs/clientlibs/granite/utils.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;/etc.clientlibs/sourcedcode/clientlibs/micro-frontend.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger" />
<h3 id="via-clientlibrary">2. Include the <strong>granite.utils</strong> as a dependency</h3>
<p>.</p>
<p>Simply ensure that your client library has a dependency for the <strong>granite.utils</strong>.</p>
<div class="mention-block">Please be aware that if you are planning to this client library approach, you are actually going to load unwanted JavaScripts apart of your page load. This includes: Sling.js, Util.js, HTTP.js, I18n.js, TouchIndicator.js, OptOutUtil.js, and the init.js (according to the <a href="http://localhost:4502/crx/de/index.jsp#/libs/clientlibs/granite/utils/js.txt" rel="noopener" target="_blank">js.txt</a> in the Adobe AEM core library). If all you want is the i18n.js, then you should try out solution #1.</div>
<p><em>In line:6, notice how the a dependency have been added.</em></p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jcr:root</span> <span style="color: #000066;">xmlns:cq</span>=<span style="color: #ff0000;">&quot;http://www.day.com/jcr/cq/1.0&quot;</span> <span style="color: #000066;">xmlns:jcr</span>=<span style="color: #ff0000;">&quot;http://www.jcp.org/jcr/1.0&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;cq:ClientLibraryFolder&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">allowProxy</span>=<span style="color: #ff0000;">&quot;{Boolean}true&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">categories</span>=<span style="color: #ff0000;">&quot;[sourcedcode.micro-frontend]&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">dependencies</span>=<span style="color: #ff0000;">&quot;[granite.utils]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></td></tr></tbody></table></div>
<p>While in this article, we have mentioned that we are using the <a href="https://www.npmjs.com/package/aem-clientlib-generator" rel="noopener" target="_blank">aem-clientlib-generator</a> for our micro-frontend, so to install it, it would be as simple as adding a new property in the clientlib.config.js, and would be something that looks like this: <strong>line:33</strong></p>
<div class="code-500">
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000066; font-weight: bold;">const</span> path <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'path'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">const</span> BUILD_DIR <span style="color: #339933;">=</span> path.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span>__dirname<span style="color: #339933;">,</span> <span style="color: #3366CC;">'dist'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">const</span> CLIENTLIB_DIR <span style="color: #339933;">=</span> path.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><br />
&nbsp; __dirname<span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'..'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'ui.apps'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'src'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'main'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'content'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'jcr_root'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'apps'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'sourcedcode'</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #3366CC;">'clientlibs'</span><br />
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">const</span> libsBaseConfig <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; allowProxy<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; serializationFormat<span style="color: #339933;">:</span> <span style="color: #3366CC;">'xml'</span><span style="color: #339933;">,</span><br />
&nbsp; cssProcessor<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'default:none'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'min:none'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; jsProcessor<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'default:none'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'min:none'</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Config for `aem-clientlib-generator`</span><br />
module.<span style="color: #660066;">exports</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; context<span style="color: #339933;">:</span> BUILD_DIR<span style="color: #339933;">,</span><br />
&nbsp; clientLibRoot<span style="color: #339933;">:</span> CLIENTLIB_DIR<span style="color: #339933;">,</span><br />
&nbsp; libs<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; ...<span style="color: #660066;">libsBaseConfig</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; name<span style="color: #339933;">:</span> <span style="color: #3366CC;">'micro-frontend'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; categories<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'sourcedcode.micro-frontend'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; dependencies<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'granite.utils'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; assets<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; js<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cwd<span style="color: #339933;">:</span> <span style="color: #3366CC;">'clientlib-site'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; files<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'**/*.js'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flatten<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; css<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cwd<span style="color: #339933;">:</span> <span style="color: #3366CC;">'clientlib-site'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; files<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'**/*.css'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flatten<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Copy all other files into the `resources` ClientLib directory</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; resources<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cwd<span style="color: #339933;">:</span> <span style="color: #3366CC;">'clientlib-site'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; files<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'**/*.*'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flatten<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ignore<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'**/*.js'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'**/*.css'</span><span style="color: #009900;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</div>
<div class="mention-block">
<strong>Still! My AEM i18n Not Showing Translations!</strong><br />
Sometimes after deploying new dictionary keys and values in your AEM platform, you will realize, in rare occasions, you&#8217;re dictionary values are not loading as expected. Have no fear, I have created an article here for how to solve this problem. Click here to learn how to refresh the <a href="/blog/aem/aem-i18n-not-showing-translations">Apache Sling I18N translation engine</a>.
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/race-conditions-with-window-granite-i18n">Race Conditions with window.Granite?.I18n</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/race-conditions-with-window-granite-i18n/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>What are AEM ExtraClientlibs?</title>
		<link>https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs</link>
					<comments>https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Tue, 20 Sep 2022 04:51:05 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Touch UI]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3796</guid>

					<description><![CDATA[<p>When dealing with cq:ClientLibraryFolder (node type), you may have come across a property called extraClientlibs. What are extraClientlibs and how does it work? As you may recall, when you want to add custom functionality or styling to the Touch Dialogues (in edit author mode, editor.html), we must include cq.authoring.editor.sites.page as a dependency and cq.authoring.editor.sites.page.hook as [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs">What are AEM ExtraClientlibs?</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When dealing with cq:ClientLibraryFolder (node type), you may have come across a property called extraClientlibs. What are extraClientlibs and how does it work?</p>
<p>As you may recall, when you want to add custom functionality or styling to the Touch Dialogues (in edit author mode, editor.html), we must include cq.authoring.editor.sites.page as a dependency and cq.authoring.editor.sites.page.hook as a category to our client library&#8217;s configuration. The configuration would look something like this:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<span style="color: #000000; font-weight: bold;">&lt;jcr:root</span> <span style="color: #000066;">xmlns:cq</span>=<span style="color: #ff0000;">&quot;http://www.day.com/jcr/cq/1.0&quot;</span> <span style="color: #000066;">xmlns:jcr</span>=<span style="color: #ff0000;">&quot;http://www.jcp.org/jcr/1.0&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;cq:ClientLibraryFolder&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">dependencies</span>=<span style="color: #ff0000;">&quot;[cq.authoring.editor.sites.page]&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">categories</span>=<span style="color: #ff0000;">&quot;[cq.authoring.editor.sites.page.hook]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></div></td></tr></tbody></table></div>
<p><a href="https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_.png"><img decoding="async" src="https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_.png" alt="Diagram of how the ccq.authoring.editor.sites will be triggered." width="100%" class="alignnone size-full wp-image-3801" srcset="https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_.png 1638w, https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_-300x116.png 300w, https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_-1024x395.png 1024w, https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_-768x296.png 768w, https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_-1536x593.png 1536w, https://sourcedcode.com/wp-content/uploads/2022/09/cq.authoring.editor.sites_.page_.click_-600x232.png 600w" sizes="(max-width: 1638px) 100vw, 1638px" /></a></p>
<p>This configuration above will evoke the client library every time the Touch UI dialogue is triggered, for all components. Illustrated above, from the author (editor.html), clicking on any component throughout the entire AEM platform, will invoke the client library.</p>
<p><strong>Benefits of using cq.authoring.editor.sites.page:</strong></p>
<ol>
<li>Client libraries created like this will be invoked for all components (when the Touch UI is open)</li>
</ol>
<hr class="spacer-large" />
<p><strong>But however, what happens if you only want your custom functionality or styling to be only called by ONE component? Well, we use the extraClientlibs property from the component&#8217;s Touch UI dialogue.</strong></p>
<p><a href="https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_.png"><img decoding="async" src="https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_.png" alt="Diagram of how the extraClientlibs will be triggered." width="100%" height="451" class="alignnone size-full wp-image-3803" srcset="https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_.png 1452w, https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_-300x93.png 300w, https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_-1024x318.png 1024w, https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_-768x239.png 768w, https://sourcedcode.com/wp-content/uploads/2022/09/my.example.clientlib.calls_-600x186.png 600w" sizes="(max-width: 1452px) 100vw, 1452px" /></a></p>
<p>Illustrated above, you will notice that adding String[] extraClientlibs to the cq:dialog (root node) of the component, the client library will only be invoked, only targeting the Touch UI dialogue that is associated to a specific component.</p>
<p><strong>Benefits of using extraClientlibs:</strong></p>
<ol>
<li>Touch UI will only invoke the targeted client library that is configured.</li>
<li>Client libraries can be created to be specifically used for a particular component.</li>
</ol>
<p><strong>Example of usage of extraClientlibs in line:6</strong></p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<span style="color: #000000; font-weight: bold;">&lt;jcr:root</span> <span style="color: #000066;">xmlns:sling</span>=<span style="color: #ff0000;">&quot;http://sling.apache.org/jcr/sling/1.0&quot;</span> <span style="color: #000066;">xmlns:granite</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/jcr/granite/1.0&quot;</span> <span style="color: #000066;">xmlns:cq</span>=<span style="color: #ff0000;">&quot;http://www.day.com/jcr/cq/1.0&quot;</span> <span style="color: #000066;">xmlns:jcr</span>=<span style="color: #ff0000;">&quot;http://www.jcp.org/jcr/1.0&quot;</span> <span style="color: #000066;">xmlns:nt</span>=<span style="color: #ff0000;">&quot;http://www.jcp.org/jcr/nt/1.0&quot;</span><br />
&nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; <span style="color: #000066;">jcr:title</span>=<span style="color: #ff0000;">&quot;Component's Granite UI&quot;</span><br />
&nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;cq/gui/components/authoring/dialog&quot;</span><br />
&nbsp; <span style="color: #000066;">extraClientlibs</span>=<span style="color: #ff0000;">&quot;[my.custom.clientlib]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">&lt;content</span><br />
&nbsp; &nbsp; <span style="color: #000066;">granite:class</span>=<span style="color: #ff0000;">&quot;cmp-teaser__editor cmp-image__editor&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;granite/ui/components/coral/foundation/container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;items</span> <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;tabs</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;granite/ui/components/coral/foundation/tabs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;items</span> <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;layout</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">jcr:title</span>=<span style="color: #ff0000;">&quot;Layout&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;granite/ui/components/coral/foundation/container&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">margin</span>=<span style="color: #ff0000;">&quot;{Boolean}true&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">sling:orderBefore</span>=<span style="color: #ff0000;">&quot;actions&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;items</span> <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;columns</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;granite/ui/components/coral/foundation/fixedcolumns&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">margin</span>=<span style="color: #ff0000;">&quot;{Boolean}true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;items</span> <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;column</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;granite/ui/components/coral/foundation/container&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;items</span> <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;layout</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">sling:resourceType</span>=<span style="color: #ff0000;">&quot;granite/ui/components/coral/foundation/form/select&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">fieldLabel</span>=<span style="color: #ff0000;">&quot;Layout&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;./layout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><br />
...</div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs">What are AEM ExtraClientlibs?</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Add AEM Custom Frontend CSS JavaScript Editor.html</title>
		<link>https://sourcedcode.com/blog/aem/add-aem-custom-frontend-css-javascript-editor-html</link>
					<comments>https://sourcedcode.com/blog/aem/add-aem-custom-frontend-css-javascript-editor-html#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 16 Jul 2022 20:51:14 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Touch UI]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3582</guid>

					<description><![CDATA[<p>I would like to re-share one of my existing blog articles on how to add a custom client library for the AEM Author Editor (editor.html). It&#8217;s a very simple configuration that requires one dependency and one client category. This method is specific to customizing page authoring in AEM. Click on this link for exactly how [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/add-aem-custom-frontend-css-javascript-editor-html">Add AEM Custom Frontend CSS JavaScript Editor.html</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I would like to re-share one of my existing blog articles on how to add a custom client library for the AEM Author Editor (editor.html). It&#8217;s a very simple configuration that requires one dependency and one client category. This method is specific to customizing page authoring in AEM.</p>
<p>Click on this link for exactly how this can be set up, <a href="https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui">https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui</a></p>
<p><strong>Usecases for adding custom CSS or JavaScript to the editor.html would be:</strong></p>
<ol>
<li>Including custom styling from the UI for the Granite UI fields.</li>
<li>Including custom Granite UI JavaScript validations such as <a href="https://sourcedcode.com/blog/aem/touch-ui/aem-richtext-max-characters-length-validation">richtext maximum characters</a> or multifield maximum items.</li>
<li>Including custom processes such as deleting component properties based on Granite UI configurations.</li>
<li>Including JavaScript injections into a page based on the window.location.pathname or searchParams.</li>
</ol>
<p>All in all, the sky is the limit on customizations that you can add to the editor.html. Click on this link for exactly how this can be set up, <a href="https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui">https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui</a></p>
<div class="mention-block">
If you want to learn how to create client libraries for the authoring environment that only target specific components checkout this article <a href="https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs">here</a>.
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/add-aem-custom-frontend-css-javascript-editor-html">Add AEM Custom Frontend CSS JavaScript Editor.html</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/add-aem-custom-frontend-css-javascript-editor-html/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Adding Custom Client Library for AEM Author Editor Touch UI</title>
		<link>https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui</link>
					<comments>https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 22 Jun 2022 19:03:57 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Touch UI]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3484</guid>

					<description><![CDATA[<p>You can use Clientlibs to extend the default implementation and add new functionality while reusing the default functions, objects, and methods. When customizing, you can create your own clientlib under / apps. Specifically, when you want to add CSS or JavaScript to your AEM Author Editor, Touch UI Dialogs, you would want to create a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui">Adding Custom Client Library for AEM Author Editor Touch UI</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>You can use Clientlibs  to extend the default implementation and add new functionality while reusing the default functions, objects, and methods. When customizing, you can create your own clientlib under / apps. </p>
<p>Specifically, when you want to add CSS or JavaScript to your AEM Author Editor, Touch UI Dialogs, you would want to create a new client library. Within your clientlib, it must have the required dependencies and categories. </p>
<div class="mention-block">
If you want to learn how to create client libraries for the authoring environment that only target specific components checkout this article <a href="https://sourcedcode.com/blog/aem/what-are-aem-extraclientlibs">here</a>.
</div>
<p><strong>The custom clientlib for AEM authoring must follow these rules:</strong></p>
<p>1. depend on the authoring clientlib cq.authoring.editor.sites.page<br />
2. be part of the appropriate cq.authoring.editor.sites.page.hook category</p>
<hr class="divider-large"/>
<h3>Example</h3>
<p>An example of a custom client library created for AEM authoring would live in this path (below), and must have the .context.xml configured like so.<br />
<strong>/apps/sourcedcode/clientlibs/clientlib-authoring</strong></p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<span style="color: #000000; font-weight: bold;">&lt;jcr:root</span> <span style="color: #000066;">xmlns:cq</span>=<span style="color: #ff0000;">&quot;http://www.day.com/jcr/cq/1.0&quot;</span> <span style="color: #000066;">xmlns:jcr</span>=<span style="color: #ff0000;">&quot;http://www.jcp.org/jcr/1.0&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">jcr:primaryType</span>=<span style="color: #ff0000;">&quot;cq:ClientLibraryFolder&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">dependencies</span>=<span style="color: #ff0000;">&quot;[cq.authoring.editor.sites.page]&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000066;">categories</span>=<span style="color: #ff0000;">&quot;[cq.authoring.editor.sites.page.hook]&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui">Adding Custom Client Library for AEM Author Editor Touch UI</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/adding-custom-client-library-for-aem-author-editor-touch-ui/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Revealing Module Pattern in JavaScript, Jquery, for AEM Client Libraries</title>
		<link>https://sourcedcode.com/blog/aem/revealing-module-pattern-in-javascript-jquery-for-aem-clientlibs</link>
					<comments>https://sourcedcode.com/blog/aem/revealing-module-pattern-in-javascript-jquery-for-aem-clientlibs#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 12 Jan 2022 03:43:17 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3150</guid>

					<description><![CDATA[<p>This is a reference to the code that I have written. I call this my version of the perfect AEM JavaScript Jquery Component that uses the Revealing Module Pattern. The Revealing Module Pattern in JavaScript is a design pattern that helps us organize our JavaScript in modules. This pattern makes it possible for us not [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/revealing-module-pattern-in-javascript-jquery-for-aem-clientlibs">Revealing Module Pattern in JavaScript, Jquery, for AEM Client Libraries</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>This is a reference to the code that I have written. I call this my version of the perfect AEM JavaScript Jquery Component that uses the Revealing Module Pattern.</p>
<p>The Revealing Module Pattern in JavaScript is a design pattern that helps us organize our JavaScript in modules. This pattern makes it possible for us not to pollute the global scope, and it also ensures that the JavaScript module is running its own code as it should.</p>
<p><strong>In this example, I have incorporated the best practices, and used special naming conventions, so that the code is organized, and set for scalability. </strong></p>
<p><a href="#code-example">Skip to the code Example: Revealing Module Pattern in JavaScript, Jquery</a></p>
<hr class="spacer-large" />
<h3>Explanation of what each line does</h3>
<ol>
<li><strong>line:1</strong> = this is a self invoking function, while passing a parameter of the window.jQuery object; all functions and variables here will be closed off from the outer scope.</li>
<li><strong>line:82</strong> = on window ready, the MyComponent.init(); public method of the MyComponent will be called.</li>
<li>&#8220;use strict&#8221; on line:3 = &#8216;use strict&#8217;; states that the JavaScript code should be executed in &#8216;strict mode&#8217;. This makes it easier to write good and secure JavaScript code.</li>
<li><strong>line:11</strong> = this will be the declaration of the Module.</li>
<li><strong>line:13</strong> = this is where the Constants will be used throughout the Module; here you can include the data-arribute names, HTML element classNames, modifier classnames, etc&#8230;</li>
<li><strong>line:20</strong> = this is where the Module level variables will be saved;
<ul>
<li>variables like $myComponent, with $ as prefix, means that these variables are holding Jquery objects.</li>
<li>variables like pingUrl, with no $, means that these variables are holding standard data.</li>
</ul>
</li>
<li><strong>line:30</strong> = notice the init() function has no under_scores like the other function. No under_score in the function name means that this is a public function.</li>
<li><strong>line:31</strong> = DO NOTHING, do not run any code when the HTML element is NOT on the page.</li>
<li><strong>line:32</strong>-34 = set Jquery objects and other types of data to the variables, declaration on line 20.</li>
<li><strong>line:36</strong> &#038;&#038; line:37 = here we register the open and close button.
<ul>
<li>functions with the under_scores, means that this is a private function.</li>
<li>functions with the without under_scores, means that this is a public function, and public functions should be exposed in line:76, from the return statement.</li>
</ul>
</li>
<li><strong>line:45</strong> = _registerOpenButton(), private function; this cannot be called outside of the Module.</li>
<li><strong>line:57</strong> = _registerCloseButton(), private function; this cannot be called outside of the Module.</li>
<li><strong>line:69</strong> = _makePingCall(), private function; this cannot be called outside of the Module.</li>
<li><strong>line:76</strong> = this return declaration is used to reveal public methods that can be called by the self invoking function. In this case, only the init() function is public.</li>
</ol>
<hr class="spacer-large" />
<h2 id="code-example">Code Example: Revealing Module Pattern in JavaScript, Jquery</h2>
<div class="code-larger">
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">;</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #3366CC;">&quot;use strict&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* MyComponent Component<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @class MyComponent<br />
&nbsp; &nbsp; &nbsp;* @classdesc initiating javascript for the MyComponent Component.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">var</span> MyComponent <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">var</span> CONST <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DATA_ATTR_PING_URL<span style="color: #339933;">:</span> <span style="color: #3366CC;">'ping-url'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_BUTTON_CLASS_NAME<span style="color: #339933;">:</span> <span style="color: #3366CC;">'.cmp-mycomponent__open-btn'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CLOSE_BTN_CLASS_NAME<span style="color: #339933;">:</span> <span style="color: #3366CC;">'.cmp-mycomponent__close-btn'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OPEN_CLASS_NAME<span style="color: #339933;">:</span> <span style="color: #3366CC;">'open'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">var</span> $myComponent <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.cmp-mycomponent'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $openBtn<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $closeBtn<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pingUrl<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Initializes the MyComponent<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @public<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>$myComponent.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $openBtn <span style="color: #339933;">=</span> $myComponent.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span>CONST.<span style="color: #660066;">OPEN_BUTTON_CLASS_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $closeBtn <span style="color: #339933;">=</span> $myComponent.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span>CONST.<span style="color: #660066;">CLOSE_BTN_CLASS_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pingUrl <span style="color: #339933;">=</span> $myComponent.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>CONST.<span style="color: #660066;">DATA_ATTR_PING_URL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _registerOpenButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _registerCloseButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Registers open button<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @private<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">function</span> _registerOpenButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $closeBtn.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _makePingCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $myComponent.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>CONST.<span style="color: #660066;">OPEN_CLASS_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Registers close button<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @private<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">function</span> _registerCloseButton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$closeBtn.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _makePingCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $myComponent.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>CONST.<span style="color: #660066;">OPEN_CLASS_NAME</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Make ping call when open or close action have been made<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* @private<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">function</span> _makePingCall<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>pingUrl<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url<span style="color: #339933;">:</span> pingUrl<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; init<span style="color: #339933;">:</span> init<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; MyComponent.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/revealing-module-pattern-in-javascript-jquery-for-aem-clientlibs">Revealing Module Pattern in JavaScript, Jquery, for AEM Client Libraries</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/revealing-module-pattern-in-javascript-jquery-for-aem-clientlibs/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>With HTL, Pass Data from AEM Backend to Javascript</title>
		<link>https://sourcedcode.com/blog/aem/with-htl-pass-data-from-aem-backend-to-javascript</link>
					<comments>https://sourcedcode.com/blog/aem/with-htl-pass-data-from-aem-backend-to-javascript#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Thu, 24 Sep 2020 22:24:37 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=2290</guid>

					<description><![CDATA[<p>When working with AEM components, it&#8217;s very common to pass data from AEM backend to a particular JavaScript event. In this article, we will be focusing on how to pass data from AEM backend using the HTML data-* attribute. HTML data attributes were designed apart of HTML5 where these attributes allow developers to store data, [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/with-htl-pass-data-from-aem-backend-to-javascript">With HTL, Pass Data from AEM Backend to Javascript</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When working with AEM components, it&#8217;s very common to pass data from AEM backend to a particular JavaScript event. In this article, we will be focusing on how to pass data from AEM backend using the HTML data-* attribute.</p>
<p>HTML data attributes were designed apart of HTML5 where these attributes allow developers to store data, as a string, on HTML elements. With HTL, we can set a value within data attribute&#8217;s value, returned from the AEM backend via Sling Model or HTL Java Use-API or HTL JavaScript Use-API. </p>
<p>The data attribute&#8217;s value set will be data from the AEM backend using the HTL scripting engine and will be apart of the HTML server-side render output. Next, JavaScript will be used to access these values.</p>
<hr class="spacer-large" />
<h2>1. HTML Data Attribute Syntax</h2>
<p><strong>The data attribute is made up of two parts:</strong></p>
<ol>
<li>The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix &#8220;data-&#8220;, example: <em>data-year</em>, <em>data-tax-code</em><em>, data-calendar-point</em>.</li>
<li>The attribute value can be any string.</li>
</ol>
<p>An example of an plain &lt;section&gt; HTML element storing multiple data attributes of values, for the use of your JavaScript logic:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&lt;section </span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tax-calculator&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp;data-year<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2020&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp;data-tax-<span style="color: #000066;">code</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span> </span><br />
<span style="color: #009900;"> &nbsp;data-calender-end-point<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;https://ms.sourcedcode.com/taxcalculator&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>section&gt;</span></div></td></tr></tbody></table></div>
<p>A HTL implementation:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;section data-sly-use.taxcal<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;com.sourcedcode.core.components.models.TaxCalculator&quot;</span><br />
&nbsp; <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;tax-calculator&quot;</span> <br />
&nbsp; data-year<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${taxcal.year @ context='scriptString'}&quot;</span> <br />
&nbsp; data-tax-<span style="color: #000066;">code</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${taxcal.taxCode @ context='scriptString'}&quot;</span> <br />
&nbsp; data-calender-end-point<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${taxcal.endPoint @ context='scriptString'}&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>section&gt;</div></td></tr></tbody></table></div>
<p>Sling Model implementation:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.components.models</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.adobe.cq.export.json.ExporterConstants</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">lombok.Getter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.DefaultInjectionStrategy</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Exporter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Resource.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>, <br />
&nbsp; &nbsp; defaultInjectionStrategy <span style="color: #339933;">=</span> DefaultInjectionStrategy.<span style="color: #006633;">OPTIONAL</span><span style="color: #009900;">&#41;</span><br />
@Exporter<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> ExporterConstants.<span style="color: #006633;">SLING_MODEL_EXPORTER_NAME</span>, <br />
&nbsp; &nbsp; extensions <span style="color: #339933;">=</span> ExporterConstants.<span style="color: #006633;">SLING_MODEL_EXTENSION</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TaxCalculator <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @Getter<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> year <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2020</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Getter<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> taxCode <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Getter<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> endPoint <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;https://ms.sourcedcode.com/taxcalculator&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h2>2. Accessing the Data Attributes with JavaScript</h2>
<p>Retrieving the value(s) of the HTML data attribute(s) in JavaScript is straight forward. You could use getAttribute() with their full HTML name to read these values, but the standard defines a better: a DOMStringMap you can read out via a dataset property.</p>
<p>To retrieve the data attribute(s) through the dataset object, retrieve the property by the part of the attribute name after data- (note that dashes are converted to camelCase).</p>
<p>Example:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000066; font-weight: bold;">const</span> article <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tax-calculator'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp;<br />
article.<span style="color: #660066;">dataset</span>.<span style="color: #660066;">year</span> <span style="color: #006600; font-style: italic;">// &quot;2020&quot;</span><br />
article.<span style="color: #660066;">dataset</span>.<span style="color: #660066;">taxCode</span> <span style="color: #006600; font-style: italic;">// &quot;3&quot;</span><br />
article.<span style="color: #660066;">dataset</span>.<span style="color: #660066;">calenderEndPoint</span> <span style="color: #006600; font-style: italic;">// &quot;https://ms.sourcedcode.com/taxcalculator&quot;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="8658919803"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/with-htl-pass-data-from-aem-backend-to-javascript">With HTL, Pass Data from AEM Backend to Javascript</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/with-htl-pass-data-from-aem-backend-to-javascript/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>How to include JavaScript for an AEM Website</title>
		<link>https://sourcedcode.com/blog/aem/how-to-include-javascript-for-an-aem-website</link>
					<comments>https://sourcedcode.com/blog/aem/how-to-include-javascript-for-an-aem-website#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sun, 19 Jul 2020 14:10:34 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Client Libraries]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=2063</guid>

					<description><![CDATA[<p>Adding behaviour to a form, button, list of items, or page scroll are some examples of front-end user interactions that we as developers must implement, to enable user rich experiences. How do we add behaviours to a page? JavaScript! JavaScript is the technology used to add user rich experiences for a website. AEM website can [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-include-javascript-for-an-aem-website">How to include JavaScript for an AEM Website</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Adding behaviour to a form, button, list of items, or page scroll are some examples of front-end user interactions that we as developers must implement, to enable user rich experiences. How do we add behaviours to a page? JavaScript! JavaScript is the technology used to add user rich experiences for a website. </p>
<p>AEM website can include &#038; run front-end JavaScript logic like all other websites. <strong>Note</strong>, There&#8217;s nothing special about an AEM web page, as an AEM page rendered output is nothing but a standard HTML document; there&#8217;s no magic or surprises. </p>
<p>In this article, we will explore the 4 techniques of how we can include JavaScript within a webpage. </p>
<div class="mention-block">
<strong>Including JavaScript By:</strong></p>
<ol>
<li><a href="#javascript-by-html-event-attributes">HTML Event Attributes</a></li>
<li><a href="#javascript-by-inline">Inline JavaScript</a></li>
<li><a href="#javascript-by-internal-client-libraries">Internal Client Libraries JavaScript</a></li>
<li><a href="#javascript-by-external-javascript">External JavaScript</a></li>
</ol>
</div>
<hr class="spacer-large"/>
<h2 id="javascript-by-html-event-attributes">1. HTML Event Attributes</h2>
<p>HTML possesses the capability to let events trigger actions in a browser, for example, executing JavaScript logic when a user clicks, hovers, drags, or keypress an element on a page. These events include onclick,  onmouseout, ondrag, oncopy, etc&#8230;</p>
<p><strong class="cnt-margin-bottom-neg-20">Use Case:</strong><br />
Quickly way to add interactions to a unique HTML element on the page, as a proof of concept. Then later implement the same functionality with a front-end JavaScript application with tests.</p>
<p><strong class="cnt-margin-bottom-neg-20">Examples:</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;back-to-top&quot;</span> <span style="color: #000066;">onclick</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.scrollTo(0,0);&quot;</span>&gt; <span style="color: #66cc66;">//</span> Execute a JavaScript when this button is clicked<br />
&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onmouseover</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.alert('mouse over')&quot;</span>&gt;Test&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt; <span style="color: #66cc66;">//</span> Execute a JavaScript when moving the mouse pointer over the &lt;<span style="color: #000000; font-weight: bold;">a</span>&gt; tag<br />
&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">onmouseout</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.alert('mouse out')&quot;</span>&gt;Test&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt; <span style="color: #66cc66;">//</span>Execute a JavaScript when moving the mouse pointer out of the &lt;<span style="color: #000000; font-weight: bold;">a</span>&gt; tag<br />
&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fullname&quot;</span> <span style="color: #000066;">onkeydown</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.alert('key down')&quot;</span>&gt; <span style="color: #66cc66;">//</span> Execute a JavaScript when a user presses a key<br />
&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fullname&quot;</span> <span style="color: #000066;">onkeyup</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.alert('key released')&quot;</span>&gt; <span style="color: #66cc66;">//</span> Execute a JavaScript when a user releases a key<br />
&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fullname&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.alert('focused')&quot;</span>&gt; <span style="color: #66cc66;">//</span> Execute a JavaScript when the input field is focused <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">selected</span><span style="color: #66cc66;">&#41;</span><br />
&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fullname&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;window.alert('blurred')&quot;</span>&gt; <span style="color: #66cc66;">//</span> Execute a JavaScript when the input field is blurred <span style="color: #66cc66;">&#40;</span>unselected<span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<h2 id="javascript-by-inline">2.Inline JavaScript</h2>
<p>Inline JavaScript can be executed by using &lt;script&gt; tag inside the &lt;head&gt; or &lt;body&gt; of the HTML document object model (DOM). Instead of specifying the source(src=&#8221;…&#8221;) of the JavaScript file in the &lt;script&gt; tag, JavaScript code can be written within the Script tag.</p>
<p><strong class="cnt-margin-bottom-neg-20">Use Case:</strong><br />
Supporting Adobe Analytics&#8217;s Data Layer JSON schema configuration values, based on the context of the page. </p>
<p><strong class="cnt-margin-bottom-neg-20">Examples:</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;!DOCTYPE html&gt; &nbsp;<br />
&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;Inline JavaScript Example&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt; <br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">h1</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;intro&quot;</span>&gt;Hello &nbsp;World&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;sly data-sly-use.dataLayer<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;com.sourcedcode.models.DataLayer&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>sly&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var digitalDataLayer <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; environment: <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; environmentVersion: <span style="color: #ff0000;">&quot;6.0.5.1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; environmentName: <span style="color: #ff0000;">'${dataLayer.environmentName @ context=&quot;scriptString&quot;}'</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; siteName: <span style="color: #ff0000;">&quot;SourcedCode.com&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; statusCode: <span style="color: #cc66cc;">200</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">target</span>: <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; page: <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageInfo: <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageName: <span style="color: #ff0000;">&quot;home&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageReferrer: document.referrer,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageSiteSection: <span style="color: #ff0000;">&quot;homepage&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageSiteSubsection: <span style="color: #ff0000;">&quot;&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageType: <span style="color: #ff0000;">&quot;homepage&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageURL: <span style="color: #ff0000;">&quot;https://www.sourcedcode.com&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt; &nbsp; <br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<h3 id="javascript-by-internal-client-libraries">3. Internal Client Libraries JavaScript</h2>
<p>This is the HTML document that references the internal JavaScript file (compiled from client libraries).</p>
<p>The most common way to declare CSS styles and JavaScript behaviour to an AEM website is the use of AEM client libraries. A Client library is very specific to AEM where it is simply configuration declaring which CSS, JS, Resource files to be bundled; a client library can also bundle other client library configurations, and it has the capability to minify and debug. It is by choice and the preferred way that all AEM sites utilise client libraries to define a website.</p>
<p><strong class="cnt-margin-bottom-neg-20">Use Case:</strong></p>
<ul>
<li>Serving JS and CSS from source files in AEM.</li>
<li>Serving organised JS and CSS from different client libraries in AEM.</li>
<li>Stitching multiple client libraries to be served in a single CSS or JS file.</li>
</ul>
<p><strong class="cnt-margin-bottom-neg-20">Example &#8211; HTL loading CSS &#038; JS using a Helper Template:</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;!DOCTYPE html&gt; &nbsp;<br />
&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;Client Librbary JavaScript file&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;!--<span style="color: #66cc66;">/</span>* Load sourcedcode libraries, css *<span style="color: #66cc66;">/</span>--&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;sly data-sly-use.clientlib<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/libs/granite/sightly/templates/clientlib.html&quot;</span><span style="color: #66cc66;">/</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;sly data-sly-call<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${clientlib.css @ categories='sourcedcode.site,sourcedcode.utils'}&quot;</span><span style="color: #66cc66;">/</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp; <br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;!--<span style="color: #66cc66;">/</span>* Load sourcedcode libraries, js *<span style="color: #66cc66;">/</span>--&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;sly data-sly-use.clientlib<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/libs/granite/sightly/templates/clientlib.html&quot;</span><span style="color: #66cc66;">/</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;sly data-sly-call<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${clientlib.js @ categories='sourcedcode.site,sourcedcode.utils'}&quot;</span><span style="color: #66cc66;">/</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</div></td></tr></tbody></table></div>
<p><strong class="cnt-margin-bottom-neg-20">Example &#8211; Result of Rendered Page:</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;!DOCTYPE html&gt; &nbsp;<br />
&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;Client Librbary JavaScript file&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-site.css&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-utils.css&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp; <br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-site.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-utils.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</div></td></tr></tbody></table></div>
<div class="modernized-box">
     <strong class="cnt-margin-bottom-neg-20">Example &#8211; Including Client Library from Editable Template Page Policies:</strong><br />
     <img decoding="async" alt="Editable template page policies" src="https://sourcedcode.com/wp-content/uploads/2020/07/editable-template-page-policies.png" /><br />
<strong class="cnt-margin-bottom-neg-20">Example &#8211; Result of Rendered Page from Editable Template Page Policies:</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;!DOCTYPE html&gt; &nbsp;<br />
&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;Client Librbary JavaScript file&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-site.css&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-utils.css&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp; <br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-site.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;/etc.clientlibs/my-site/clientlibs/clientlibs-utils.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</div></td></tr></tbody></table></div>
</div>
<hr class="spacer-large"/>
<h3 id="javascript-by-external-javascript">4. External JavaScript</h2>
<p>This is the HTML document that references the external JavaScript file.<br />
<strong class="cnt-margin-bottom-neg-20">Use Case:</strong></p>
<ul>
<li>Referencing the Adobe Dynamic Tag Manager (DTM) library to support Adobe DTM features.</li>
<li>Referencing to the Adobe Analytics library, to support Adobe Analytics features.</li>
<li>Referencing third party libraries, to support libraries features.</li>
</ul>
<p><strong class="cnt-margin-bottom-neg-20">Example:</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">&lt;!DOCTYPE html&gt; &nbsp;<br />
&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;External Librbary JavaScript file&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css&quot;</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt; &nbsp; <br />
&nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;<br />
&nbsp; &nbsp; &lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt; &nbsp;<br />
&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-include-javascript-for-an-aem-website">How to include JavaScript for an AEM Website</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/how-to-include-javascript-for-an-aem-website/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
