0

Please no jQuery advice and I am sorry to ask this question but nobody from other websites that are similar like stack overflow has not been able to solve this yet and it's very important I find an answer to this soon I been very persistent on

finding an answer that gives a desirable result. Here's the situation. I need to be able to append an ajax response at the same time allow execution of JavaScript in each ajax response without any complications or unnecessary additional retriggering of JS in them. This is what I tried and what I notice

  • innerHTML +=

causes interruptions of contents inside the ajax response for example iframes restarts and JS restarts but still appends the ajax response.

  • outerHTML +=

causes no interruptions or refreshing of contents outside the ajax response location like iframes etc.. but Javascript does not execute properly or don't work at all depending on what method is used to give an ajax response page the ability to execute JavaScript in them.

  • insertAdjacentHTML

causes no interruptions of contents inside the ajax response for example iframes don't restart and JS don't restart but still appends the ajax response but causes unnecessary additional script execution to occur more than once inside a ajax response per ajax response overtime for example first time I call an ajax response it works perfectly second time the JS executes two times at that moment the third time the JS of that ajax response executes three times etc. which should execute one time per call of each ajax response.

Any time I get close, something undesirable like those traits occur from those methods so i'm wondering is it the methods or the way I structure them or maybe I need another method to do this on how I want it to act.

I simply just need to append an ajax response with no iframes or JS etc.. from being interrupted or restarting over each time a new ajax response comes along and appends and I need to be able to only be able to execute JavaScript inside those ajax response page once by each ajax response call not more than once regardless if there is multiple script tags in that ajax response page it will execute all the scripts tags on that page once per ajax call.

I wonder what other methods are there.

This is my code without any of those failed methods.

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;
        }

        button{
        display: block; 
        }

        #random{
        margin-top: 20px;
        margin-bottom: 20px;
        }

 </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').//Method to give a desirable appending effect described in the description???

          //Allow JS in the #ajax div* regardless if there is more than one script tag???

        }
      }

       xhr.open('POST','b.php'); 
       xhr.send();
    }

 });

        </script>

        <button id='executeAjax'>Execute</button>


        <button id='random'>random</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>

<script>

document.querySelector('#random').addEventListener('click',random);

function random(){
alert('random');
}

</script>
  • Sounds strange. Isn't appendChild not just working? – Adrian Sep 07 '18 at 06:06
  • 1
    I don't see how Javascript wil 'restart' if you add some HTML to a page. If you add HTML however, it causes a redraw. And if it is a lot, you could see a bit of flickering. Maybe that's your 'interruptions of contents' – Michel Sep 07 '18 at 06:10
  • @michel From the description he is replacing the content with something like innerHtml = innerHtml + newContent. -> this will trigger again previosly added content – Adrian Sep 07 '18 at 06:14
  • Thanks for your reply @Michel but it's what I notice with different methods I come across. Some methods interrupts the iframe especially if that iframe has a video running from YouTube and stops it and then I have to press the play button again because another ajax response has just been appended to the page other methods don't stop it but they also have other cons to it I just need another method do you know any? If so can you provide an example I will really appreciate that. –  Sep 07 '18 at 06:19
  • @Adrian thanks for your reply and I read on any other methods that I can find on google I try to apply that method but I can't find any examples of appendChild that is relating to ajax and I failed at trying to put that with ajax can you provide an example to see how that will look like? –  Sep 07 '18 at 06:21
  • @fsofb "insertAdjacentHtml" is propably the function you need. Than you still need something to execute the JS. https://jsfiddle.net/2tc6bksq/14/ (not working is jsfiddle, try local) https://stackoverflow.com/questions/24222074/dynamically-created-script-not-executing – Adrian Sep 07 '18 at 06:29
  • Try it with `createDocumentFragment`. It is the least intrusive, because it doesn't cause a page reflow. [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Document/createDocumentFragment) – Michel Sep 07 '18 at 06:31
  • @adrain I very confused I don't see how that will work with my ajax example I try your method and use what I know and I don't see how that can solve my situation I'm not asking for you guys to code for me but if you can give me an example with my code I will understand better in a ajax way I learn best that way –  Sep 07 '18 at 06:41
  • @Michel thanks for providing that link but it does not make sense to me on how I can apply that to my situation in an ajax way i'm not asking for you guys to code for me but an example will help if that is possible with my code Michael if you don't know how that will structure with my code it's Ok I just appreciate that you try to help me out. –  Sep 07 '18 at 06:45
  • See my comment in your previous question https://stackoverflow.com/questions/52212651/working-method-to-allow-js-to-run-in-ajax-responsetext-with-insertadjacenthtml - let me see if I understand what you want. The HTML you get using the button will have the same javsacript in it every time, but you only want to run that javascript the first time - so, either follow advice of my last comment in your other question, or, refactor your code so it doesn't send that javascript twice – Jaromanda X Sep 08 '18 at 03:16

0 Answers0