Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

global javascript variable removed after Touch UI page loads

Avatar

Level 1

In our javascript we create a global object that is used by many of our components. It seems to work fine when viewing the page without "editor.html" in the path; however when viewed with "editor.html" the variable is created and then removed entirely. So far I have not been able to figure out what is causing our global object to disappear; however the variable _g is present when "editor.html" is in the path and Granite is being used. Is there a setting in our site that is causing Touch UI to remove global objects created by our javascript code? 

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

I was able to figure out the root cause of your issue. When you open the page for editing with "editor.html" then AEM includes the page content inside an iframe.The iframe is contained inside the child window object of the main page. The parent window object includes all authoring controls like editing toolbar on the top and other elements required for authoring. 

And when you access the page without the "editor.html" in the URL then there is no iframe and the page contents are part of the main window object. So it is an issue of scoping of the variable. You declared the global variable inside the iframe window and it will not be accessible outside the iframe window to the parent window object. 

You need to declare your variable such a way that it is accessible to both child and parent windows- 

//Example //Check if current window is not an iframe. if(self==top) {            _varName = window._varName || {};     } else {            _varName = window._varName || {};           window.parent._varName = _varName;     }

View solution in original post

7 Replies

Avatar

Employee Advisor

May be there is a name conflict when you view the page with "editor.html" in path. Have you tried renaming the variable and test with "editor.html" ? 

Avatar

Level 1

It doesn't seem like a naming conflict since the variable is removed completely. I tried creating other variables at the same point where this one is created and those disappear after continuing javascript execution as well. 

Avatar

Administrator

Hi 

There is already an issue for Missing Scripting Variables.

Link:- https://docs.adobe.com/docs/en/aem/6-1/release-notes/known-issues.html ( Reference number "CQ-40324" )

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

Avatar

Level 1

This isn't related to my issue, as my bug is not related to Sightly scripting variables. 

Avatar

Employee Advisor

Can you share your code where you have declared the global variable ?   

Avatar

Level 1

_varName = window._varName || {}

 

the object exists once it's declared, but it will go away afterward. this does not happen when viewing the normal page without editor.html

Avatar

Correct answer by
Employee Advisor

I was able to figure out the root cause of your issue. When you open the page for editing with "editor.html" then AEM includes the page content inside an iframe.The iframe is contained inside the child window object of the main page. The parent window object includes all authoring controls like editing toolbar on the top and other elements required for authoring. 

And when you access the page without the "editor.html" in the URL then there is no iframe and the page contents are part of the main window object. So it is an issue of scoping of the variable. You declared the global variable inside the iframe window and it will not be accessible outside the iframe window to the parent window object. 

You need to declare your variable such a way that it is accessible to both child and parent windows- 

//Example //Check if current window is not an iframe. if(self==top) {            _varName = window._varName || {};     } else {            _varName = window._varName || {};           window.parent._varName = _varName;     }