I read and I tried many methods from posts and from Google on how to get the AJAX .responseText
to allow JS to run and none of the methods either don't work or gave glitchy results I need a working method that works with insertAdjacentHTML
properly.
code
a.php
<style>
#main {
max-height: 800px;
width: 400px;
display: -webkit-flex; /* Safari */
-webkit-flex-direction: column-reverse; /* Safari 6.1+ */
display: flex;
flex-direction: column-reverse;
overflow: auto;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function(){
document.querySelector('#executeAjax').addEventListener('click', sendAjax);
function sendAjax(){
var xhr= new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
document.querySelector('#ajax').insertAdjacentHTML('beforeend', xhr.responseText);
}
}
xhr.open('POST','b.php');
xhr.send();
}
});
</script>
<button id='executeAjax'>Execute</button>
<div id="main">
<div id='ajax'></div>
</div>
b.php
<style>
iframe{
display: block;
width: 100px;
height: 100px;
}
</style>
<script>
alert('Hello');
</script>
<script>
alert('Hello again');
</script>
<iframe src="https://www.youtube.com/embed/tgbNymZ7vqY"></iframe>