views:

100

answers:

6

If you do script src="/path/to/nonexistent/file.js" in an HTML file and call that in a browser, and there are no dependencies or resources anywhere else in the HTML file that expect the file or code therein to actually exist, is there anything inherently bad-practice about doing this?

Yes, it is an odd question. The rationale is the developer is dealing with a CMS that allows custom (self-contained) javascript files to be provided in certain circumstances. The problem is the CMS is not very flexible when it comes to creating conditional includes for javascript. Therefore it is easier to just make references to the self-contained js files regardless of whether they are actually at the specified path.

Since no errors are displayed to the user, should this practice be considered a viable option?

+2  A: 

Well the major drawback is performance since the browser will try (hard) to download the file and your server will look for it. Finally the browser may download the 404 page instead - thus slowing down the page load.

Jakub Hampl
+1  A: 

If your web server is configured to do work on a 404 error ("you might be looking for this", etc) then you're also causing unnecessary load on the server.

great_llama
+1  A: 

If you have the script referred to in the <head> tag, ( not recommended for starters ), it will slow down the initial page-render time somewhat too.

If instead of quickly returning a 404, your site just accepts the connection and then never responds, this can cause the page to take an indefinite amount of time to load, and in some cases, lock up the entire user interface.

( At least that was the case with one revision of FireFox, I hope they've fixed it since I saw that happen ~2 years ago.* )

You should at least put the tags as low in the page order as you can afford to to remedy this problem.

Your best bet by far is to have one consistent no-op url that is used as a fill-in for all "doesn't exist" JavaScript files, that returns a 0-byte response with HTTP headers telling the UA to cache it till the cows come home, that should negate most your server<->client load penalties beyond the first hit ( and that should hardly hurt people even on ye-olde dialup )

*Lesson learned: don't put script-src references in head, especially for 3rd-party scripts hosted outside your machine, because then you can have the joy of having clients be able to access your website, but run the risk of the page being inoperable because of a bit of advertising JS that was inaccessible due to some internet weirdness. Even if they're a reputable-ish 3rd-party.

Kent Fredric
A: 

If you choose to implement it this way, you could tune the web server that if the referenced JS file is not found, instead of 404, it could return a redirect (301) to an empty/default JS file.

sibidiba
+1  A: 
  1. you should ask yourself why you were too lazy to test this yourself :)

  2. i tested 1000 randomized javascript filenames and it took several nanoseconds to load, so no, it doesn't make a difference. example:

script src="/7701992spolsky.js"

This was on my local machine however, so it should take N * roundTripTime for the browser to figure out for remote servers, where N is the number of bad scripts.

  1. If however, you have random domain names that don't exist, like

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

then it will take a long f-in time.

el chief
You also run the delightful risk of somebody registering that domain and putting something malicious there =). JOY~!
Kent Fredric
@chief: Too lazy?! Egads no! I knew you were waiting to teach me! Why let a cub fumble around when you can learn directly from the chief? ;)
dreftymac
A: 

If you are using asp.net you can look into using custom handlers (ASHX files). Here's 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 your html header you could declare a file pointing to the custom handler:

src="/js/javascripthandler.ashx"