2

You are using onAfterRender event which is not suitable for modifying the document object because this event is triggered after the page has already been rendered. Use onAfterDispatch, onBeforeRender or onBeforeCompileHead instead.


2

$document->addScriptDeclaration() method is working, no problem with that. window.event is deprecated: https://developer.mozilla.org/en-US/docs/Web/API/Window/event instead of that you should use document.addEventListener() or window.addEventListener() on the event of DOMContentLoaded https://developer.mozilla.org/en-US/docs/Web/API/Window/...


Only top voted, non community-wiki answers of a minimum length are eligible