Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to access a js function inside dialogs

Avatar

Level 4

Hi, 

i need to make a shared function with javascript. If i import the file inside the header of my page, the same function is not visible inside a dialog. It seems to be a different scope.

Is there a way to do it?

Thanks

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

You shouldn't be including javascript on the pages for dialog or any authoring related customizations. Rather you should create a JS client library and define category property of the client library node to "cq.widgets" for classic UI and "cq.authoring.dialog" for touch UI. 

Examples-

For touch UI - /libs/cq/gui/components/authoring/dialog/image/clientlibs/image

View solution in original post

13 Replies

Avatar

Level 10

You are importing a piece of JS code in header of your page ?

how are trying to access the JS code in dialog?

Avatar

Correct answer by
Employee Advisor

You shouldn't be including javascript on the pages for dialog or any authoring related customizations. Rather you should create a JS client library and define category property of the client library node to "cq.widgets" for classic UI and "cq.authoring.dialog" for touch UI. 

Examples-

For touch UI - /libs/cq/gui/components/authoring/dialog/image/clientlibs/image

Avatar

Level 4

Thanks kunal.

If i add the category cq.widgets the libraries are automatically charged during the starting of cq, right?

So this function will be available inside all of my projects? I need this function visible for only my current project.

Avatar

Employee Advisor

You can add a condition in your JS function to not to execute if the path is not specific to the project. CQ.WCM.getPagePath() will give you the path of the page in the javascript function. 

Avatar

Level 10

use cq:widgets only if its part of aem library, else if that JS code is part of your project code then use your own clientlib and make sure you load this clientlib only in project where you need this...

Avatar

Level 4

I know only this way to load a clientlib: <cq:includeClientLib categories="myclientlib"/>

My dialog can be accessed under the siteadmin and the code inside the jsp isn't executed.

I can't use the cq:widgets because this code must be launched only in one project (i can't use a condition inside the function due to a project constrain)

Avatar

Level 10

Can you give a brief of what you are trying to achieve or implement

Avatar

Level 4

Are you working with TouchUI or Classic?

I have specific javascript for dialogs in my touchUI interface with the extraClientLibs property in my _cq_dialog node.

This is how I have added JavaScript to only specific dialogs. If the javascript function your trying to access is declared in another clientlib, then that clientlib should have the category cq.authoring.dialog/cq.widgets as mentioned above.

Avatar

Level 4

Thanks Tyler, i'm working with the Classic UI.

The property extraClientLibs is exactly what i need, therefore a method to load js and css during the loading of the dialog.

But i can't find information about it and seems that doesn't work. If i access the dialog across the sidekick or across the siteadmin i can't see any trace about the static resources.

Avatar

Level 2

Looks like solution to problem suggested is to use listener or dialog event to load clienlibs/js+css.

+ Points:- Not dependent on page jsp or siteadmin or site.

              The js will get loaded  before the dialog opens and gets rendered.

- Points:-The js events may not bind properly but it basically depends on js library included.

Avatar

Level 4

My actual solution:

function(dialog) { if(document.getElementById('jquery')==null){ //avoid the loading 2 times var path = window.location.href; var lang = path.split("/")[6]; var design="/etc/designs/myproject/"+lang+"/"; console.log("Script for siteadmin imported"); var fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", design+"plugin/js/jquery-1.11.3.min.js"); document.head.appendChild(fileref); } else{ console.log("Script for siteadmin already imported"); } }

How can i use a clientlib inside the js code?

Avatar

Level 10

You use <cq:includeClientLib categories="myclientlib"/> if you do not want to use the predefined client lib folders - that are applicable to all AEM projects. Those are your only options. 

You are right - you place <cq:includeClientLib categories="myclientlib"/> in a JSP - such as a component JSP so that the clientlib loads. When you click on the dialog of a component - the JS methods are available.

If you are trying to create a dialog from site admin and the JSP is not fired and the client libs do not load, you will have to place your JS in the predefined clientlibs - as mentioned in this thread. 

Avatar

Level 2

I think you have done it right, the only modification we have to do is to load clientlibs path instead of jquery file .

So instead of plugin/js/jquery-1.11.3.min.js  just use your clintlibs path which is basically based on the category name provided in clientlibrary folder.