I learned to use XMLHttpRequest to load on to the same page but I found that is better to use Fetch API, can anyone recommend me some guide?

تبليغ
سؤال

يرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.

تبليغ
‎إلغاء

I’m starting to make a personal website for my web proyects, my page in the way I learn to do things is using a XMLHttpRequest to load the sections on to the same page, but searching online I learned that is better to use the Fetch API, but I don’t know if is really that different or better for what I use it for.

I will at the basic structure of the function that I use for the load:

var loader = document.getElementById(“filler”);
var nav_proj = document.getElementById(“nav_project”);

function load_projects(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
loader.innerHTML = this.responseText;
}
};
xmlhttp.open(“GET”, “projects.php”, true);
xmlhttp.send();
}

nav_proj.addEventListener(“click”, load_projects, false);
<!– Simple menu when you click projects with loads a set of images of the projects –>
<header class=”topnav” id=”top-nav”>
<nav>
<a href=”#” id=”nav_home”>Home</a>
<a href=”#” id=”nav_project”>Projects</a>
<a href=”#” id=”nav_cont”>Contact</a>
</nav>
</header>

<!– The empty div to fill with the projects.php –>
<div id=”filler”></div>

‫أضف إجابة

تصفح
تصفح

مجهول يجيب