<script> tags in report widget templates are not executed when dashboard loads #818
-
Winter CMS Builddev-develop PHP Version8.0 Database engineMySQL/MariaDB Plugins installedNo response Issue descriptionIf there is a <script> tag in the template of a report widget, and that report widget is added to the dashboard, then the scipt is not executed. This was working fine in October CMS 472. If the script is moved to an external JS file and added in the loadAssets() of the report widget, then it cannot interact with the DOM elements defined in the template, as at the time of the asset loading and executing the partial is not loaded yet. So there is no working way currently to use JS in a report widget which interacts with the DOM elements defined in the template. Steps to replicateCreate a report widget with this template:
then add this report widget to the dashboard. Upon visiting the dashboard the widget's content should be "after script run", but instead it stays "before script run". If the user opens the properties panel of the report widget by clicking on the cogwheel icon and then closes it (without changing anything), then the content changes to "after script run". WorkaroundIf the user opens the properties panel of the report widget by clicking on the cogwheel icon and then closes it (without changing anything), the script defined in the <script> runs. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Have you tried using an event listener so the script waits until the DOM has been rendered before trying to modify DOM elements? Something like: document.addEventListener('DOMContentLoaded', () => {
// your code here...
const testElement = document.getElementById("test");
testElement.innerHTML = 'after script run';
}); |
Beta Was this translation helpful? Give feedback.
-
It is still the same that way if I use in the template. Even if I put a single I tried to put that into the external JS and load it via loadAssets(), but still the div#test does not exists when it runs and I get javascript error:
|
Beta Was this translation helpful? Give feedback.
-
Report widgets are brought in by an AJAX call, and both the original framework and Snowboard use What you must do is load the JavaScript in an external asset with jQuery$(document).on('render', function () {
if (!$('#test').length) {
return;
}
$('#test').html('after script run');
}); SnowboardSnowboard.on('ajaxDone', () => {
if (document.getElementById('test')) {
document.getElementById('test').innerHTML = 'after script run';
}
}); |
Beta Was this translation helpful? Give feedback.
Report widgets are brought in by an AJAX call, and both the original framework and Snowboard use
innerHTML
writes to write the HTML of the report widget into place. One of security measures ofinnerHTML
is that it does not execute inline JavaScript, so yes, using JavaScript in your template will not work.What you must do is load the JavaScript in an external asset with
addJs
, as you mentioned doing, and depending on whether you use jQuery or Snowboard, listen for the event that fires after AJAX requests are completed:jQuery
Snowboard