0
Good day I tried to add an HTML snippet I created to another full HTML file But the js script of the html snippet didn't import.
Any alternatives for importation??
6 ответов
+ 1
have any code to share ? so we can get the better image of the problem and what you're trying to do
+ 1
you're creating an infinite loop right there by calling includeHTML() again after making a request. remove that and you're good.
other than that, make sure to call includeHTML the first time after the page load. window load listener, or calling it at the very bottom of the body.
i tested it myself the code are working correctly aside what i describe above. pretty neat concept btw
good job
0
function includeHTML() {
var z, i, elmnt, file, xhttp;
/* Loop through a collection of all HTML elements: */
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
/*search for elements with a certain atrribute:*/
file = elmnt.getAttribute("w3-include-html");
if (file) {
/* Make an HTTP request using the attribute value as the file name: */
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();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/* Exit the function: */
return;
}
}
}
0
<div w3-include-html="Nav.html"></div>
0
Nav.html
<h1>Recipe-world</h1>
</div>
<div id="img">
<div id=lines>
☰
</div>
<div id="lnk"><input type="search" id="inp" value="recipe for" /></div>
<div id= "pages" >
<div id="lnk" ><a href="#">recipe for </a><br /></div>
<div id="lnk" > <a href="#">recipe for a food </a><br /></div>
<div id="lnk" ><a href="#">recipe for</a><br /></div>
<div id="lnk" > <a href="#">recipe for </a><br /></div>
<div id="lnk" > <a href="#">recipe for </a><br /></div>
<div id="lnk" > <a href="#">recipe for </a><br /></div>
<div id="lnk" ><a href="#">recipe for </a><br /></div>
<div id="lnk" > <a href="#">recipe for </a><br /></div>
<div id="lnk" > <a href="#">recipe for </a><br /></div>
<div id="lnk"><a href="#">recipe for </a><br /></div>
<div id="lnk" ><a
0
The nav.html js won't work when exported