I’m glad you have your solution!
No the linked solution does not use AJAX, it utilizes Node & the express library to fetch a remote local file and pre-render that file while binding POSTed data to the markup. The file is written in a markup pattern ejs that a template engine module in Node understands allowing you to bind data directly to the markup. This is known as templating; There are JavaScript libraries that existed (mustache, handlebars) allowing you to do this in regular HTML markup using attributes, but that practice seems to have decayed away and has joined the Node ecosystem.
In reality this is possible outside Node as well in languages rendered on the server before being sent to the client which if you know PHP (I do) is similar. A PHP file pre-renders, and you can bind POSTed data, or data sent through the HTTP query string to the markup.
Aside from pre-rendering, our next best-solution is to load the HTML itself. Naturally JS has no access to the file-system, yet with the XMLHttpRequest object (AJAX - Asynchronous JavaScript & XML) we can define a URI to query, initiate the query, and define a callback to be called at any given request response (there are four with associated id’s if I’m not mistaken from 0-4). The HTML file itself should be empty, acting as a skeleton or template. JavaScript can do this while the page is already loaded using AJAX (Asynchronous Javascript & XML).
If you loaded an HTML file you might get an HTML string of that files contents. You can parse that string into it’s own DOM. DOMParser was the best way to do this last time I researched myself for my own purposes. This allows you to traverse the files node tree and bind the data yourself. You could add your id’s through the file to make this easier, though if you know the the structure this might not be necessary.
You might consider giving the markup in these files id’s similar to the identifiers you would be passing to them in the template file yourself such as ‘#title’ or ‘#content’. Better yet, you should be using Data Attributes. A data attribute is a fancy term for a general attribute naming convention that allows developers to bind their own data to HTML markup. Rather than searching for ID’s in the DOM, you query these attributes:
<div id="title'></div>
A data attribute name pattern uses data as a prefix for your custom attribute name (data-attribute="value"
).
<div data-section="title"></div>
And so you can query your markup like so:
document.querySelector('[data-section="title"]');