user1788736 user1788736 - 5 months ago 16
Ajax Question

Checking if a m3u8 URL is broken using Javascript

I am using the following code to determine if an m3u8 URL is broken or not. I test with two different URLs, one that is online and one that is offline. For both cases, my javascript function doesn't alert me that file is found or not and firefox debugs doesn't give me any error but status variable always shows 0. Could anyone tell me what I am doing wrong here?


for offline url i get this header response(in httpfox developer tool) :HTTP/1.1 404 Not Found
for online url i get this header response(in httpfox developer tool) :HTTP/1.1 200 OK


<script type="text/javascript" src=""></script>
function testFunction() {


//now we checking if the file exist

UrlExists(m, function(status){
if(status === 200){
// file was found
alert('file found'+m);

else if(status === 404){
// 404 not found
alert('file not found'+m);


function UrlExists(url, cb){
url: url,
dataType: 'text',
type: 'GET',
complete: function(xhr){
if(typeof cb === 'function')
cb.apply(this, [xhr.status]);


}// end of main


<button onclick="testFunction()">Click me</button>

Answer Source

This will not work if you are using external URL's as CORS (Cross-origin resource sharing) will kick in and will stop you as you are not on the same domain.

Working version: local files only

UrlExists('/path/file.php', function(status){
    if(status === 200){
       alert('file found');
    else if(status === 404){
       alert('file not found');

Unfortunately, there isnt a valid way of doing this through Javascript. However, you can conduct this through backend functionality e.g. PHP

$file = '';
$file_headers = @get_headers($file);

if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
else {
    $exists = true;

If you build this into a php function and then use your Ajax functionality to pass the URL through to the PHP for validation, and then return back a response - it should work.

Edit: Curl Example

$mainUrl = curl_init($url);
curl_setopt($mainUrl,  CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($mainUrl);

$httpCode = curl_getinfo($mainUrl, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
    echo "404 Error";
}else if($httpCode == 200){
    echo "200 Error";
    echo "all good - sort off...";


Here is the Curl option - Now the way i would do it (and i really had to do it...) is looping through each and every single URL on the page (in js) and sending it as an object to PHP (through Ajax). With PHP, i would use the above CURL functionality to confirm which ones are broken (either with a 1 or 0) and then send a response back.