Hack 61. Eliminate Annoying Browser Stalls
Here's a method to short-circuit the part of the web page that is stalling your browser.
It doesn't happen often, but every once in a while, you visit a web page that seems to take forever to load, or simply hangs and never finishes loading. The problem often occurs because the web page includes a picture, a button, an advertisement, or some other web element from another server that your browser cannot reach or cannot reach quickly.
These resources are slow to respond for a few reasons. Sometimes there's a bottleneck on the Internet itself. Sometimes a link to a button, picture, or ad points to an Internet site that is overloaded and having trouble responding.
Sometimes a link to an off-site graphical element is pointing to an address that is inexplicably difficult to resolve. You see, for every domain name, such as oreilly.com, there are one or more numeric Internet protocol (IP) addresses. Your browser needs to ask a domain name server (DNS) for that IP address, and that server might have to pass the request on to another server, and then to another. Sometimes there's a bad link in the chain, and your browser simply gets hung up waiting to find out the numeric IP address.
But when you visit a web page again and again for weeks and notice that it consistently stalls, that's a sign that one or more identifiable elements, such as a graphic or advertisement, might be causing the stall. For this hack, assume the problem is with a graphical button. In this case, the web site you are trying to view does not contain the graphics file needed for that button to be displayed. Instead, the web page includes an HTML instruction to get the graphic for that button from another site. You can run into trouble if the browser has trouble finding the IP address for the site that contains the graphic. Or perhaps your browser doesn't have fast access to the site that provides the graphic, or your browser is blocked from accessing that site. The bottom line is that your browser can stall when loading the web page simply because it cannot get the graphical element from a remote site. Some browsers might even refuse to finish loading the page until the problem is resolved.
You can short-circuit this process so that it never has to contact the problematic web site. The trick revolves around how your computer translates domain names, such as oreilly.com, to IP addresses.
The /etc/nsswitch.conf file determines how your computer tries to resolve the address of a domain name. Examine your /etc/nsswitch.conf file, and look for the line that starts with the label hosts. For example, it might look like this:
hosts: DNS files
This tells your computer to check the DNS first, and if it cannot find the IP address from the DNS, to try looking up the IP address in your local file called /etc/hosts.
If the line looks more like the following, it checks the local file /etc/hosts first, and then checks with a DNS if it can't find the IP address from your local file:
hosts: files DNS
In most cases, browsers are slow to load pages or stall on something such as a graphical element, because the DNS is having trouble resolving the domain name into an IP address.
8.8.1. Short-Circuit the DNS Request
You can short-circuit this process in a few steps. First, make sure the line in your /etc/nsswitch.conf file looks like the latter example, where files precedes DNS. If your /etc/nsswitch.conf file places something other than files first, rearrange the order to make sure files comes first.
Next, determine the domain name that is causing your browser to load a page slowly or get stuck loading a web page element. Here's how to track down the troublesome site. Keep your eye on the status bar in your browser. If you are experiencing the kind of problem I've been describing, the status bar usually reads something like "Looking up www.someadvertisementsite.com" or "Waiting for www.someadvertisementsite.com" while is it slow or stuck. The status message won't change until it can find or contact that site. (Be careful not to move the mouse while you are waiting for the site to load, or you might inadvertently clear this status message even though the problem continues to exist.)
Suppose your browser is getting stuck loading a page, because it is having trouble trying to figure out the IP address of a host such as www.someadvertisementsite.com. That's because it cannot find the IP address in your /etc/hosts file, and it has to resolve the address using DNS.
Now edit your /etc/hosts file and add a line that defines the problematic web site so that it points to the IP address for your localhost (your own machine):
That's all you need to do. The next time you browse the web page, your browser will assume the graphic, button, advertisement, or other problematic element resides on your own machine. It will fail (quickly) to find what it needs, and then move on to finish loading the page.
You can also use this technique to block advertisements. Many domain names are dedicated exclusively to serving advertisements. If you list those domain names in your /etc/hosts file and make them point to your own machine, your browser won't load the advertisement. I don't recommend doing this, however. This ad-blocking technique isn't nearly as effective as it once was. For one thing, many advertisers have figured out how to get around this short-circuit technique, and it simply won't work. For another, ads are often useful instead of annoying. But if you're determined to block advertisements, plenty of more-effective methods are available, such as ad-blocking extensions for various browsers, or other ad-blocking tools that run in the background.