Hướng dẫn pure javascript append html
For example,
Result: "new content" is appended in page but there will be no alert saying Hello World! I have tried innerHTML, insertAdjacentHTML, append, appendChild methods. But the problem is JS and CSS not loading dynamically. In jQuery,
is there any equivalent for pure javascript?
Mr. Polywhirl 36.6k12 gold badges76 silver badges124 bronze badges asked Apr 11 at 16:27
You can use
Side note, for your case, there is no way called safety to convert strings to scripts because somebody may inject malicious scripts into your strings and execute them without your notice, so you need to make sure all your strings are under your control properly. You can check XSS attack for a better understanding. answered Apr 11 at 16:53
Nick VuNick Vu 11.9k2 gold badges17 silver badges27 bronze badges 0 If your are sure about
Working example answered Apr 16 at 22:05
lissettdmlissettdm 10.9k1 gold badge12 silver badges35 bronze badges You have to call your alert when something happens, if you want the alert to be displayed when you append the HTML, then wrap it in a self executing function:
answered Apr 11 at 16:32
2 Scripts cannot be added directly to the DOM as HTML text and executed. I would separate the action into two phases. Insert the HTML and add the script to the end of the document body as a contextual fragment.
|