Export (0) Print
Expand All

JavaScript and JScript Auto-proxy Example Files

The following scripts provide examples of how you can use a .pac file to specify an automatic proxy URL. To use these functions, you must change the proxy names, port numbers, and IP addresses. This topic contains the following:

Example 1: Local hosts connect directly, all others connect using a proxy

The following function checks to see whether the host name is a local host, and if it is, whether the connection is direct. If the host name is not a local host, the connection is through the proxy server (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 (periods) in the host name. If so, it returns false. Otherwise, the function returns true.

Example 2: Hosts inside the firewall connect directly, outside local servers connect using a proxy

The following function checks to see whether the host is either a "plain" host name (meaning that the domain name is not included) or is part of a particular domain (.company.com), but that the host name 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";

}

noteNote
The localHostOrDomainIs function is run only for URLs in the local domain. The dnsDomainIs function returns true if the domain of the host name matches the domain given.

Example 3: If host is resolvable, connect directly — otherwise, connect using a proxy.

The following function requests the DNS server to resolve the host name passed to it. If it can, then a direct connection is made. If it cannot, the connection is made using a proxy. This is useful when an internal DNS server is used to resolve all internal host names.

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.

Example 4: If host is in specified subnet, connect directly — otherwise connect using a proxy.

The following function compares a given IP address pattern and mask with the host name. This is useful if certain hosts in a subnet should be connected directly and others should be connected using a 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).

Example 5: Determine connection type based on host domain

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.

Example 6: Determine connection type based on protocol being used

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.

Example 7: Determine proxy setting by checking to see if host name matches IP address

The following function makes a proxy selection by translating the host name 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.

Example 8: If host IP matches specified IP, connect using a proxy — otherwise, connect directly

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 get the numeric IP address explicitly (Example 7 uses the dnsResolve function to translate the host name 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.

Example 9: If there are any dots in the host name, connect using a proxy — otherwise, connect directly.

The following function checks to see how many dots (periods) are in the host name. If there are any dots in the host name, make a connection using a proxy. If there are no dots in the host name, make a direct connection. This is another way to determine connection types based on host name 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 host name.

Example 10: Specify days of the week to connect using a proxy, other days connect directly

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 then 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] [,GMT] ) function returns whether the current system time falls within the range specified by the parameters day1, day2, and GMT. Only the first parameter is required. The GMT parameter presumes time values are in Greenwich Mean Time rather than the local time zone.

Additional references:

  • For more information about additional resources and references for Windows® Internet Explorer® Administration Kit 8, see References.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft