How to access a js function inside dialogs | Community
Skip to main content
Level 4
February 5, 2016
Solved

How to access a js function inside dialogs

  • February 5, 2016
  • 13 replies
  • 6304 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kunal_Gaba_

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

13 replies

Level 4
February 12, 2016

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?

smacdonald2008
Level 10
February 12, 2016

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. 

Sunil_Chowdhary
Level 2
February 12, 2016

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.