Tony Li
Tony Li

Reputation: 147

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?

Upvotes: 5

Views: 2360

Answers (1)

thriqon
thriqon

Reputation: 2488

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.

Upvotes: 10

Related Questions