Treat 404 in html5 video

1

How can I handle errors like 404 in the html5 video tag? I took a look at all available events and the only one that should serve in this case was the onerror , however, when I define it by means of an attribute in the tag. The same can never find my function and in the example of the link there, when it is 404, it does not call the function.

Does anyone know how to get around 404 errors?

Follow the example in jsfiddle . To test the first function is only for "onerror = test ()" in the video tag

    
asked by anonymous 18.05.2016 / 16:11

1 answer

4

If it is to check whether a video exists or not, it may be best to check this before loading the video tag. If the video exists you mount the tag, otherwise mount an error message in HTML for example.

To check if a file exists on the internet and is reachable by who is browsing your page, you can do this:

No happiness jQuery:

function videoExisteEEhAlcancavel(urlVideo)
{
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status == 200;
}

With happiness jQuery:

$.ajax({
    url: urlDoVideo,
    type:'HEAD',
    error: function()
    {
        // grande chance de ser 404, mas pode ser 500.
        // você pode obter mais info se adicionar parâmetros
        // a este método e lê-los.
    },
    success: function()
    {
        // arquivo existe e é alcançável.
    }
});

Rouched Adapted from:

link

    
18.05.2016 / 16:50