1

I load entire page html with ajax call, display it with document.write and then I do some modification. I need to use document.write to load elements in the right order in the same way as it was loaded by visiting the page. Everything works fine, except on IE and Edge where setInterval doesn't work.

My code:

var my_module = {  
  loadPage: function(){                                                        
      var url = window.location.origin;    
      $.ajax({                                   
         type: "GET",                           
         url: url                                     
       }).done(function(data) {
            document.write(data);                
            document.close();
            my_module.example()  
      });                                       
  },  
  example: function(){
    alert(1) //this works fine
    var waitForElement = setInterval(function(){
      alert(5) //this won't work in IE or edge
    },2000) 
  },                                            

}      
my_module.loadPage();

In other browsers I receive alert, but not in IE and Edge. What is the reason for that and is there any solution for it?

JohnyFree
  • 1,309
  • 2
  • 19
  • 33
  • Possible duplicate of [Why is document.write considered a "bad practice"?](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – Phil Jul 17 '19 at 04:21
  • @Phil this is not duplicate at all. The question you mentioned doesn't say anything about setInterval problem. I am not asking if document.write is bad or good practice, I am asking why setInterval doesn't work in IE with document.write. I have own reasons why I use document.write, but again the question is not about that. – JohnyFree Jul 17 '19 at 11:12
  • You should read the answer, not the question – Phil Jul 17 '19 at 20:19

1 Answers1

0

I think it might due to the different ways of execution of document.write() in different browsers. You could check this documentation. Please also check this thread for more information. If you want to load some elements in page, you could use appendChild() as a workaround.

Yu Zhou
  • 6,874
  • 1
  • 4
  • 20