Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a download link is working without downloading the file

Is there a way in Javascript (and jQuery) to check if a URL is status 200 (specifically not 404) without downloading it's contents if it is in fact status 200?

For example, I want to check if a download link to a video works before executing some code. The issue when I use $.ajax() is that when the link does in fact work, it will only notify me AFTER the download has finished. In a way is there a way to just "ping" the url to see if it works without getting it's contents?

like image 937
Tony Li Avatar asked Jan 10 '23 19:01

Tony Li


1 Answers

You can try a HEAD request, which should accomplish what you are trying to do:

jQuery.ajax({type: "HEAD", url: "http://google.com/"})

This type of request is mainly used by browsers to check cache validity, but it also should work here.

like image 91
thriqon Avatar answered Jan 12 '23 08:01

thriqon