Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Custom JS Variables

Avatar

Level 2

I am trying to write a selector that spits bunch of custom JS variables. As author uses frames, those variables are not available. But publish seems to fine as there are no frames. How does CQ.WCM or CQ_Analytics objects are available? How I can I achieve the same thing in my selector js.jsp file. I appreciate your detailed response. thank you.

Selector example (/content/xxx/en/home.html, head will generate <script type="text/javascript" src="/content/xxxx/en_US.globalconfig.js"></script>

page/basePage/globalconfig.js.jsp

{code}---------------------

<%@include file="/apps/fishersci/components/shared/global.jsp" %>
<%
%>

var globalCQConfigs = window.globalCQConfigs ||{};

// design path

globalCQConfigs.publicRootDir = "${publicRootDir}";
globalCQConfigs.pageLocale = "${pageLocale}";
globalCQConfigs.localePath = "${localePath}";

---------------------

1 Accepted Solution

Avatar

Correct answer by
Level 10
  1. Create a clientlib and include all objects.  (In sitecatalyst includes CQ_Analytics objects are defined at library /etc/clientlibs/foundation/sitecatalyst)
  2. in your component script include the client lib defined above ( In sitecatalyst it is at sitecatalyst.jsp)
  3. Decide the selector in the script ( In sitecatalyst selector is picked at sitecatalyst.jsp)

View solution in original post

7 Replies

Avatar

Correct answer by
Level 10
  1. Create a clientlib and include all objects.  (In sitecatalyst includes CQ_Analytics objects are defined at library /etc/clientlibs/foundation/sitecatalyst)
  2. in your component script include the client lib defined above ( In sitecatalyst it is at sitecatalyst.jsp)
  3. Decide the selector in the script ( In sitecatalyst selector is picked at sitecatalyst.jsp)

Avatar

Level 2

Need little more clarification please. Objects are generated in JSP as JS. is it possible to make a client lib thats going to be generated on the fly?

Avatar

Level 10

bswx wrote...

Need little more clarification please. Objects are generated in JSP as JS. is it possible to make a client lib thats going to be generated on the fly?

 

<script type="text/javascript" src="<%= clientlibResource.getPath() %>.<%= "genarate_clientlib_as_per_your_requirement" %>.js"></script>

Avatar

Level 2

Sham I appreciate your time. Still not clear actual solution though.

Say the on page template selector is globalCQConfigs.js.jsp  i.e <head> contains .. <script tag calls globalCQConfigs.js> thus auto generates the js file.   (/apps/xxxx/components/page/globalCQConfigs.js.jsp)

Generating script tag is not the problem, but how can we make the selector js.jsp file into clientlib?

Avatar

Employee

bswx wrote...

Generating script tag is not the problem, but how can we make the selector js.jsp file into clientlib?

 

You can't. A client library is essentially static. What you want is to dynamically generate JavaScript, which is perfectly fine, just has nothing to do with clientlibs.

Your original post mentioned something about frames, which seems to be a problem specific to your JavaScript (and not how it is generated). What do you mean by "those variables are not available"? "Available" in what context?

Avatar

Level 2

Problem : I want to make some global values available to Javascripts, for example WCMMode is available using CQ.WCM.WCMMode. But these variables will be be coming from various SS objects, configs etc, I want to make that available for client side scripts so they can accessible to perform some tasks. 

Attempted Solution: So using selectors I know how to generate a JS file with values as I need from server side as above. But these are accessible on publish side as there are no frames. Because this script is inside a frame in author, the other scripts wont be able to access them. But I see CQ.WCM.WCMMode available in author. Hope this clarifies .. 

Thank you for your help!.

Avatar

Employee

bswx wrote...

Problem : I want to make some global values available to Javascripts, for example WCMMode is available using CQ.WCM.WCMMode. But these variables will be be coming from various SS objects, configs etc, I want to make that available for client side scripts so they can accessible to perform some tasks. 

Attempted Solution: So using selectors I know how to generate a JS file with values as I need from server side as above. But these are accessible on publish side as there are no frames. Because this script is inside a frame in author, the other scripts wont be able to access them. But I see CQ.WCM.WCMMode available in author. Hope this clarifies .. 

Thank you for your help!.

 

 

 

 

I guess I'm not understanding what the issue is with the frames. The "other scripts" should be in the same frame (whether on author or publish), so there shouldn't be an issue. And again, the fact that the JavaScript is dynamically generated isn't relevant to the browser.

The only thing which would be different between author and publish is that in author (from a script running in the site page), window.top != window. So if you do have a script which does window.top.globalCQConfigs, that wouldn't work. But I don't see why you would be doing this.

Regards,

Justin