Can someone explain to me about the HTML snippets? See the image below (w3schools HTML snippets tutorial). Then I create a "content.html" file according to w3schools instructions. Then I create another .html file and then use the same code but don’t show any content to the display. Can someone explain me how to use it?
When I try to my computer –
>Solution :
There is actually an error that occurred when loading your page that should that you can find in your console log similar to
html.html:1
Access to XMLHttpRequest at ‘file:///content.html’ from origin ‘null’ has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, edge, https, chrome-untrusted.
This occurs as a result of the file being loaded using the file protocol rather than the HTTP protocol, due to the absence of a web server. As browsers do not allow you to load local files using the File protocol using an HTTP request. Which is what the code below is trying to do.
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
/*remove the attribute, and call this function once more:*/
elmnt.removeAttribute("w3-include-html");
includeHTML();
}
}
To get this to work on your local system you would have to set up a web server. And access the HTML file using localhost. Depending on your intentions for implementing this you could look for a web server that will be suitable. However, I would advise Apache Server, using XAMPP which is relatively easy to install.


