Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Really basic question, but I'm hoping to get some clarity here.

Let's say I have a static website with some html, some css and basic javascript. No web app to back it up. When testing the website locally, why do I need to run a web server to serve the content to the browser?

Is it because the relative paths in the URLs used in web pages are relative to the folder where the server is retrieving the content from and thus the various files included from the central HTML page can't quite get located on the file-system?

share|improve this question
2  
A good example of when a static website works without a webserver is when you save a website, either from a browser (save page as) or with a tool like httrack or wget. Those cases do most of the things you're asking about. – Journeyman Geek Jan 30 '13 at 2:15
up vote 5 down vote accepted

Can static websites be viewed without a server?

Yes.

No Surprise

When testing the website locally, why do I need to run a web server to serve the content to the browser?

You don't.

Is it because the relative paths in the URLs used in web pages are relative to the folder where the server is retrieving the content from and thus the various files included from the central HTML page can't quite get located on the file-system?

No.

share|improve this answer
    
Just to clarify, I'm aware that you can load any page on disk into your browser. However, will the local files linked in the html (e.g. <link href="css/mycss.css [...]/> or <script src="js/foo.js" [...]</script>) be referenced correctly? Is there a way to have the same URL work for both the way you showed, and when the same website is served through a server? – glitch Jan 30 '13 at 2:08
    
@glitch It should just work. Your browser's security settings might be getting in the way -- i.e. it might disallow access to local files from a website. – ta.speot.is Jan 30 '13 at 2:09
1  
Great, thanks for taking the time to put that together :) – glitch Jan 30 '13 at 2:25
1  
Back in ancient times before we added a CGI shopping cart, this was the way we tested for broken links. Xenu link sleuth on a local disk copy beat wasting time on the slow internet of the day. After implementing the shopping cart, we had to build an internal test server for running the CGI efficently for testing. – Fiasco Labs Jan 30 '13 at 2:53
2  
@glitch // is protocol-relative, so I think you'll end up with file://[whatever]/ajax.googleapis.com/.... Which is what you're asking for. – ta.speot.is Jan 30 '13 at 6:39

Static websites can be viewed without a server, locally, simply by opening them up in a web browser, and you can do whole big websites this way if you want. But there are limitations and drawbacks, which is why people and systems use local servers for static websites.

One of the reasons you may be asking is because using many of the popular static site generators, like Jekyll, requires that you tell the system to --serve (or similar) it, creating a local server for the static site. But if it's static, why do you need a server?

Here are a few reasons:

  • only relative links are safe (/ as a link to home is broken, as is referencing /css/something.css, since / is the root of your computer, not the site);
  • relatedly, directory-level links fail (blog/ shows the files under that directory, instead of looking for blog/index.html or similar);
  • and browsers treat local files differently among themselves and between then and hosted files (Chrome limits ajax calls, IE uses a different protocol from everyone else, etc.).

So, no, you don't need it, but you do need it if you want those things.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .