Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the availability of a resource using JavaScript?

If I have a URL to a video file, how can I detect if the resource pointed by the URL is valid and exists before it can be displayed? I've seen some answers suggesting AJAX, but I only know AJAX to send and retrieve some data, not to get the status of the file whether it exists or not.

For example, if I have a URL like http://www.example.com/video.mp4, how could I check whether video.mp4 exists or not and can or cannot be retrieved?

like image 920
Chen Li Yong Avatar asked Feb 01 '15 11:02

Chen Li Yong


People also ask

What is the search resource availability API in field service?

In the latest Field Service v8.8.x/Universal Resource Scheduling v3.8.x update, we released the Search Resource Availability API for scheduling single requirements. A list of available resources and their time slots can be retrieved by passing the details of a single resource requirement to this API as input.

What is the difference between search resource availability and create requirement group?

The Search Resource Availability for Requirement Group API returns available time slots for resources when you search by using requirement groups. The Create Requirement Group Bookings API uses the available time slots for resources to generate booking records for your requirement groups.

Which version of Dynamics 365 has the search resource availability API?

Applies to: Dynamics 365 Organization 9.0+ with Universal Resource Scheduling 3.8.x version In the latest Field Service v8.8.x/Universal Resource Scheduling v3.8.x update, we released the Search Resource Availability API for scheduling single requirements.

What is resource availability in project management?

Resource availability refers to the information about what resources you can use to service projects, when, and under what conditions. This information is critical, because on every project, the ability to succeed depends on whether you have access to the essential resources and tools.


2 Answers

You don't really need ajax, just create a video element, and see if it can load the source

var video = document.createElement('video');

video.onload = function() {
    alert('success, it exist');
    // show video element
}

video.onerror = function() {
    alert('error, couldn\'t load');
    // don't show video element
}

video.src = 'http://www.example.com/video.mp4';

Different browsers play different formats, to check if the file can be played in the current browser, you can use the canplaythrough event

video.oncanplaythrough = function() {
    alert("This file can be played in the current browser");
};

if the file is on the same domain, and ports and protocol match, you can use ajax to do a HEAD request and see if the resource exists, but that won't work cross-domain

var http = new XMLHttpRequest();
http.open('HEAD', '/folder/video.mp4');

http.onreadystatechange = function() {
    if (this.readyState == this.DONE) {
        if (this.status != 404) {
          // resource exists
        }
    }
};

http.send();
like image 137
adeneo Avatar answered Oct 20 '22 01:10

adeneo


You can send a HEAD request. HEAD requests send back only the HTTP headers, so if you get a status of 200 or 304 it means the resource exists, if you get a 404 it means the resource doesn't exist.

like image 43
Cristik Avatar answered Oct 20 '22 01:10

Cristik