The following 10 scripts provide examples of how a .pac file could be used to specify an auto-proxy URL. To use these functions, you must change the proxy names, port numbers, and IP addresses.
Notes
The following function checks to see whether the hostname is a local host, and if it is, whether the connection is direct. If the hostname is not a local host, the connection is through the proxy (proxy).
function FindProxyForURL(url, host) { if (isPlainHostName(host)) return "DIRECT"; else return "PROXY proxy:80"; }
The isPlainHostName() function checks to see if there are any dots in the hostname. If so, returns false, else returns true.
The following function checks to see whether the host is either a "plain" hostname (meaning the domain name is not included) or part of a particular domain (.company.com) but the hostname is not either www or home.
function FindProxyForURL(url, host) { if ((isPlainHostName(host) || dnsDomainIs(host, ".company.com")) && !localHostOrDomainIs(host, "www.company.com") && !localHostOrDoaminIs(host, "home.company.com")) return "DIRECT"; else return "PROXY proxy:80"; }
Notes
The following function asks the DNS server to try to resolve the hostname passed to it. If it can, then a direct connection is made. If it cannot, the connection is made via proxy. This is useful when an internal DNS server is used to resolve all internal hostnames.
function FindProxyForURL(url, host) { if (isResolvable(host)) return "DIRECT"; else return "PROXY proxy:80"; }
See note on the isResolvable() function at the top of the page.
The following function compares a given IP address pattern and mask with the hostname. This is useful if certain hosts in a subnet should be connected directly and others should be connected via proxy.
function FindProxyForURL(url, host) { if (isInNet(host, "999.99.9.9", "255.0.255.0")) return "DIRECT"; else return "PROXY proxy:80"; }
See note on the isInNet() function at the top of the page.
The isInNet(host, pattern, mask) function returns true if the host IP address matches the specified pattern. The mask indicates which part of the IP address to match (255=match, 0=ignore).
The following function specifies a direct connection if the host is local. If the host is not local, this function determines which proxy to use based on the host domain. This is useful if the host domain name is one of the criteria for proxy selection.
function FindProxyForURL(url, host) { if (isPlainHostName(host)) return "DIRECT"; else if (shExpMatch(host, "*.com")) return "PROXY comproxy:80"; else if (shExpMatch(host, "*.edu")) return "PROXY eduproxy:80"; else return "PROXY proxy"; }
The shExpMatch(str, shexp) function returns true if str matches the shexp using shell expression patterns.
The following function extracts the protocol being used and makes a proxy selection accordingly. If no match is made on the protocol, then a direct connection is made. This is useful if the protocol being used is one of the criteria for proxy selection.
function FindProxyForURL(url, host) { if (url.substring(0, 5) == "http:") { return "PROXY proxy:80"; } else if (url.substring(0, 4) == "ftp:") { return "PROXY fproxy:80"; } else if (url.substring(0, 7) == "gopher:") { return "PROXY gproxy"; } else if (url.substring(0, 6) == "https:") { return "PROXY secproxy:8080"; } else { return "DIRECT"; } }
The substring() function extracts the specified number of characters from a string.
The following function makes a proxy selection by translating the hostname into an IP address and comparing it to a specified string.
function FindProxyForURL(url, host) { if (dnsResolve(host) == "999.99.99.999") { // = http://secproxy return "PROXY secproxy:8080"; } else { return "PROXY proxy:80"; } }
See note on the dnsResolve() function at the top of the page.
The following function is another way to make a proxy selection based on specifying an IP address. This example, unlike Example 7, uses the function call to explicity get the numeric IP address (Example 7 uses the dnsResolve() function to translate the hostname into the numeric IP address).
function FindProxyForURL(url, host) { if (myIpAddress() == "999.99.999.99") { return "PROXY proxy:80"; } else { return "DIRECT"; } }
The myIpAddress() function returns the IP address (in integer-dot format) of the host that the browser is running on.
The following function checks to see how many dots are in the hostname. If there are any dots in the hostname, make a connection via proxy. If there are no dots in the hostname, make a direct connection. This is another way to determine connection types based on hostname characteristics.
function FindProxyForURL(url, host) { if (dnsDomainLevels(host) > 0) { // if the number of dots in host > 0 return "PROXY proxy:80"; } return "DIRECT"; }
The dnsDomainLevels() function returns an integer equal to the number of dots in the hostname.
The following function determines the connection type by specifying days of the week that are appropriate for a proxy. Days that do not fall between these parameters use a direct connection. This function could be useful in situations where you might want to use a proxy when traffic is heavy, and allow a direct connection when traffic is light.
function FindProxyForURL(url, host) { if(weekdayRange("WED", "SAT", "GMT")) return "PROXY proxy:80"; else return "DIRECT"; }
The weekdayRange( <day1> [,<day2>] [,<fGMT>] ) function returns whether the current system time falls within the range specified by the parameters <day1>, <day2>, and <fGMT>. Only the first parameter is necessary. The GMT parameter sets the times to be taken in GMT rather than in the local time zone.
Note
Where the function is called with <day1> == <day2>, previous versions of Microsoft Internet Explorer would yield results different from Netscape NavigatorÖ. Specifically, previous versions of Internet Explorer would interpret this day range as an entire week, while Microsoft Internet Explorer 4.0 and Netscape NavigatorÖ interpret the range as a single day. For example, if the current day is Monday, the call weekdayRange("TUE", "TUE") returns TRUE on previous versions of Internet Explorer and FALSE on Microsoft Internet Explorer 4.0 and Netscape NavigatorÖ.