Are there any negative consequences of looking for a javascript file that doesn't actually exist?

If you make the script src = "/path/to/nonexistent/file.js" in the HTML file and call it in the browser, and there are no dependencies or resources elsewhere in the HTML file that are expecting the file or code to actually exist Is there something inherently poor practice?

Yes, this is a strange question. The rationale is that the developer is dealing with CMS, which allows you to create certain (stand-alone) javascript files under certain circumstances. The problem is that CMS is not very flexible when it comes to creating conditional inclusions for javascript. Therefore, it is easier to simply refer to stand-alone js files, regardless of whether they are really on the specified path.

Since errors are not displayed to the user, should this practice be considered viable?

+7
source share
6 answers

If you have the script specified in the <head> (not recommended for starters), this will also slow down the initial page rendering time.

If instead of quickly returning 404, your site simply accepts the connection and then never answers, this can lead to the fact that the page will take an unlimited amount of time to load, and in some cases block the entire user interface.

(At least it was in the case of one FireFox revision, I hope they fixed it, since I saw that this happened ~ 2 years ago. *)

At the very least, you should put tags in the order of the pages, as you can solve this problem.

It’s best to use one consecutive no-op URL, which is used as padding for all “nonexistent” files. JavaScript that returns a 0-byte response with HTTP headers telling UA to cache it until the cow returns home, which should adversely affect most of your client errors on the server, and not for the first time (and this is unlikely to hurt people even at the dock)

* Lesson learned: do not put script -src links in your head, especially for third-party scripts located outside your machine, because then you can rejoice that customers will be able to access your site, but the risk that the page will be inoperable due to for some JS ads that were unavailable due to some kind of internet weirdness. Even if they are supporters of the third grade.

+1
source share

Well, the main drawback is performance, as the browser will try to download the file and your server will look for it. Finally, the browser can load 404 pages, which slows down page loading.

+2
source share

If your web server is configured to work with error 404 ("you can search for it," etc.), you also cause unnecessary load on the server.

+1
source share
  • you should ask yourself why you were too lazy to check it yourself :)

  • i tested 1000 randomized javascript file names and it took a few nanoseconds to load, so no, that doesn't matter. Example:

script src = "/7701992spolsky.js"

This was on my local machine, so for the browser you need to use N * roundTripTime to find out for remote servers, where N is the number of bad scripts.

  1. If, however, you have random domain names that do not exist, for example

script src = "http://www.randomsite7701992.com/spolsky.js"

then it takes a long f-time.

+1
source share

If you decide to implement it this way, you can configure the web server so that if the referenced JS file is not found, instead of 404 it can return a redirect (301) to an empty / standard JS file.

0
source share

If you are using asp.net, you can learn about custom handlers (.ashx files). Here is an example:

 public class JavascriptHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; //Some code to check if javascript code exists string js = ""; if(JavascriptExists()) { js = GetJavascript(); } context.Response.write(js); } } 

Then in the html header you can declare a file pointing to a custom handler:

 src="/js/javascripthandler.ashx" 
0
source share

All Articles