To execute JavaScript specifically for the AEM Author Environment, developers can add a condition to check for a Cookie stored in the browser, ‘cq-authoring-mode’; this works on AEM 6.5+. When the Cookie of ‘cq-authoring-mode’ exists, then execute JavaScript during authoring on the editor.html. Only in the Author Environment, the Cookie, ‘cq-authoring-mode’, will exist. Below is a code snippet demonstrating how to check for the existence of the ‘cq-authoring-mode’ 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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Import the necessary modules import Cookies from 'js-cookie'; // Check if the 'cq-authoring-mode' key exists in cookies const authoringModeCookie = Cookies.get('cq-authoring-mode'); if (authoringModeCookie) { // Exit the JavaScript logic if the key is found console.log('Authoring mode cookie found. Exiting JavaScript logic.'); } else { // Continue with your JavaScript logic for AEM Author Environment console.log('Authoring mode cookie not found. Proceeding with JavaScript logic.'); // Add your custom functionality here } |
In this example, we use the ‘js-cookie’ library to handle cookies in a simplified manner. The Cookies.get(‘cq-authoring-mode’) function retrieves the value of the ‘cq-authoring-mode’ key from cookies. If the key exists, the JavaScript logic exits gracefully. Otherwise, you can proceed with implementing your custom functionality.
Benefits of Customized JavaScript in AEM Editor
- Context-Aware Functionality: 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.
- Enhanced Authoring Experience: Custom JavaScript allows for the augmentation of the authoring experience by introducing features that streamline workflows and improve usability for content authors.
- Graceful Handling of Conditions: Checking for the existence of keys in cookies enables developers to gracefully handle conditions, ensuring that the JavaScript logic behaves appropriately in different scenarios.
Conclusion
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.

