Check url content type with javascript Check url content type with javascript ajax ajax

Check url content type with javascript


Make an Ajax call with a head request.

var url = window.location.href;var xhttp = new XMLHttpRequest();xhttp.open('HEAD', url);xhttp.onreadystatechange = function () {    if (this.readyState == this.DONE) {        console.log(this.status);        console.log(this.getResponseHeader("Content-Type"));    }};xhttp.send();


FYI if your server doesn't allow HEAD requests but does allow GET requests, you can do this by limiting the range of the GET request.

var xmlhttp = new XMLHttpRequest();xmlhttp.open("GET", url, true);xmlhttp.setRequestHeader("Range", "bytes=0");xmlhttp.onreadystatechange = function () {  if (this.readyState == this.DONE) {    console.log(this.getResponseHeader("Content-Type"));  }};xmlhttp.send();