We have a component that contains the following script block to insert a js observable image that gets updated when it comes into view:
<script type="text/javascript">
document.currentScript.parentNode.insertAdjacentHTML('afterbegin', '<span class="js-dom-observable js-dom-observable--picture"><img src="http://www.fillmurray.com/200/300" data-src="http://www.fillmurray.com/500/500" alt="Image"></span>');
</script>
This works well when the component is loaded directly onto the page, however, when it is in the html as a result of an ajax call and appended to the page, it no longer adds the image - I'm guessing it's because the document.currentScript
is no longer the script tag it is in but the script that is executing the ajax:
$.ajax({
url: `/${APP_NAME}${filterResultEndpoint}`,
type: 'POST',
traditional: true,
data: searchData.getPostData(),
success: result => {
$('.results').html(result);
},
});
Is there a way to make this insertAdjacentHTML
in the script tag work in an ajax call - if I replace the insertAdjacentHTML
with a simple document.write
, it will work in both cases, but I'm unable to use that as it will fail our coding analysis policies