Is it possible to add DOM elements using Javascript to create a web page?

In PHP, you can include other files (for example, a header or footer file - I will call them partial) to help serve a specific page, which allows you to minimize page mismatch. However, HTML does not have this feature. I implemented a solution whereby JavaScript at the end of the page adds static partial parts after the document has declared that it is ready:

 $(function() { html = "<div id=\"navbar\"><h1>Cool Webpage!</h1></div>"; $("#header").append(html); }); 

#header is the <div> , which is the very top of the <header> - it is used as the target node, where #navbar will be introduced into.

As far as I can tell, this is a possible workaround to mitigate inconsistencies between pages, but I feel that I'm a little naive.

Is it possible to implement this method, or is there a better way? Can I add AJAX to HTML and add it on the fly? Are there any problems that I have to follow if you implement this method?

+5
source share

Source: https://habr.com/ru/post/1242571/


All Articles