Is it possible to check to see if a file/page exists via JavaScript but prevent the 404 Error from showing up in the console?
Asked
Active
Viewed 3.8k times
108
-
Why do you care if the 404 error appears in the console? Developers (and other tech savvy types) are the only ones who will ever open the console. – Matt Ball Aug 12 '11 at 04:11
-
11Because when scanning the console for errors while debugging, it would be great if these specific 404 errors (that are expected) were muted. – Muers Aug 12 '11 at 04:13
-
2At least in Chrome, there does not seem to be a way to suppress these errors in the console. – Matt Ball Aug 12 '11 at 04:33
-
5You will have to make a server side script that takes a filename as a parameter and checks if it exists. Then load that script with AJAX – Paul Aug 12 '11 at 04:47
-
you want to check the file on client side or server side?? – Ankit Aug 12 '11 at 08:41
-
1There are specific cases where it would be gr8 to check on client side if file exists on server. For example when writing unit testing framework, it would be good to check if specified file (module) with tests exists on server before loading, without throwing error. And polluting console is ugly, and in this case undesired. – yoosiba May 07 '12 at 14:51
-
2Polluting the console is more than ugly; it also takes up memory and makes things slower and slower the more output accumulates in there. – Don Hatch Feb 11 '16 at 19:13
-
There is a solution (if you can control the backend). See here: https://stackoverflow.com/a/47165242/3119283 – ShQ Nov 07 '17 at 19:40
1 Answers
74
Seems like the answer is: No. Can't avoid getting a 404 error in the console unless you kick off a call to a server-side script to check file existence.

Muers
- 3,190
- 3
- 26
- 32
-
1You can catch error, but it is still logged into console, so looks like throw is somewhere in native implementations, and only catch of re-thrown error is possible. Or maybe error is thrown after logging. Anyway, couldn't find solution to that neither. – yoosiba May 07 '12 at 14:54
-
1making the request inside a try{} catch{} helps mask the error in IE, but not other browsers. – BishopZ Feb 15 '13 at 17:49
-
22
-
5@aleclarson It really does. It should be valid to try to request a resource, have it come back as not available, and catch that as an exception. – Rob May 24 '20 at 18:19
-
Years after the question, the answer is finally [yes](https://stackoverflow.com/a/75848002/343721). – Jan Turoň Mar 26 '23 at 15:36