I want to load dynamically content on my page using promise in javascript.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Main</title>
<script type="text/javascript">
function func(url){
return new Promise(function(resolve, reject) {
var request = new XMLHttpRequest();
request.open('GET', url);
request.onload = function() {
if (request.status == 200){
resolve(request.response);
} else {
reject(Error(request.statusText));
}
};
request.onerror = function() {
reject(Error("Network Error"))
};
});
}
$(document).ready(function() {
func('hello.html');
});
</script>
</head>
<body>
</body>
</html>
I have hello.html file in the same folder and want to load it using func(). Is it the correct way to do it?