If you serve javascript with a 404 status does it still get executed?
Wed 7 October 2015This topic came up at work today when we noticed a Javascript file was missing. Does the browser still try to execute the 404 page that gets returned? And if not, would it if the Content-Type were set appropriately?
I set up a little test page like:
<script src="js.cgi?status=404&ctype=text/javascript">
where js.cgi outputs text like:
document.write("Executed javascript for status=404 and content-type=text/javascript");
so that I could test whether the code for a given Content-Type and status got executed.
Content-Type didn't make any difference in any of my tests (i.e. text/html and text/javascript both got Ignored or executed just the same).
Firefox executed any Javascript served with a 2xx status code, but not for 4xx or 5xx. Chrome executed any Javascript served with a 2xx or 3xx status code, but not for 4xx or 5xx.
With the 3xx test, I returned the content exactly the same as for a 2xx response, including the lack of a Location header, just to see how the browsers would react.
Neither browser executed code for 1xx statuses, or for 6xx.
Both browsers executed code for completely fictitious statuses, as long as they fell in 2xx (or 3xx for Chrome) such as 293.
So the answers to our questions are: no the browser does not execute the 404 page, and no it wouldn't even if it had a javascript Content-Type.
I found all this interesting. If you can shed more light on it, please give us a shout.
Cheers.
If you like my blog, please consider subscribing to the RSS feed or the mailing list: