Conditionally Display a Single Line of JavaScript in AEM
October 8, 2024
This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.
The other day I needed to call a JavaScript function only when a certain condition is not true.
Oh, and this was in a script tag in an HTML file within the ui.apps
section of Adobe Experience Manager.
I ended up with something like the following:
<script>
// some code
${condition ? "" : "console.log('this is false!')" @context='scriptString'}
// even more code
</script>
In my case, the condition was a property for the page that was set.
We probably shouldn’t have so much JavaScript in the ui.apps
section, but we do. It should probably be in the ui.frontend
section, but it’s not.
I just have to work with what we have.
Leave a Reply
Thank you. Your comment will be visible after approval.
Your comment has been submitted, but their seems to be an error. Check your browser console for more details.