1

JavaScript – Update HTML Code

 11 months ago
source link: https://cwestblog.com/2023/05/19/javascript-update-html-code/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

JavaScript – Update HTML Code

Published by Chris West on May 19, 2023

Have you ever used JSBin or CodePen and wondered how you can accomplish something like they do on those sites? Of course, CodePen is much more advanced and has more options but if you ever want to combine JavaScript code and CSS code into a full or partial HTML document you can use the updateHTMLDoc() function that I defined below:

To make this function I am leveraging the parseFromString() function of the DOMParser class to parse the given HTML code so that it can be modified. Here is an example showing how you can use this function to add CSS and JS to the head of the given HTML string:

Hopefully this short example helps you see how you can use this function. I have also added JSDoc annotation to make it easier to use this function in an IDE such as Visual Studio Code.

Happy coding! 😎


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK