Click to return to the HTML Help home page    
Web Workshop  |  DHTML, HTML & CSS  |  HTML Help

Link to a File Outside Your Help System


Microsoft Corporation

Updated June 10, 1999

There are many reasons why you might want to link to a file located outside of a compiled help (.chm) file. Say you want to link to a video clip. Including such a large file in your help project could result in an enormous compiled help file. There may also be times when you need to link to files containing last-minute information that was not ready when the .chm file was built.

When you are designing a normal Web page, linking to files is a simple matter of specifying the location of the target file. The location of the page containing the link is irrelevant. However, things are different in a compiled help file. The main problem is that you can only link to external files using an absolute path. This is fine if you are certain the location of the help file will always be the same. But what if a user installs your help file to an unexpected location? If this happens, any links using absolute paths will be broken. Similarly, if your help file is to be referenced from a compact disc, it is impossible to predict which drive letter will be in use.

You can use JScript to bypass these limitations. The following script uses the location object to find out where your .chm is located on the local computer, and parses an absolute path to the files you specify. All you need to do is install the target file(s) into the same directory as the .chm, and specify the target filenames in each respective anchor tag.

Following is a step-by-step explanation of how the script works.

Here, the function is begun and the variables are declared. Variable "fn" will be passed to the script from the link itself:

<SCRIPT Language="JScript">
function parser(fn) {
var X, Y, sl, a, ra, link;

The JScript search method is used to find the index of the first colon in the string returned by the location object:

ra = /:/;
a = location.href.search(ra);

An IF statement determines which of the two possible moniker protocols is in use. If the value of "a" equals 2, then mk:@MSITStore: is the protocol in use and X equals 14 (used later to parse an offset of 14 characters). Otherwise, protocol ms-its: is in use and X equals 7.

if (a == 2)
X = 14;
else
X = 7;

Next, the JScript lastIndexOf method returns the index of the last backslash in the location.href string to use as a value for Y (the second character offset, used later in parsing). Variable "sl" contains the substring to search for (two backslashes are used to indicate a single backslash):

sl = "\\";
Y = location.href.lastIndexOf(sl) + 1;

This is where the values X and Y come into play. The substring method uses X and Y to parse the beginning and end of location.href, respectively, returning a substring (hence the name of the method). For example, say location.href returns the value ms-its:C:\dir\file.chm::/topic.htm. In the substring method, if X equals 7, then the first 7 characters are removed from the string. If Y equals 14, then everything after the 14th character is removed. This would result in C:\dir\, the root path of file.chm. The string "file:///" is then added to the beginning of the string, and the target filename (variable "fn") is added to the end:

link = 'file:///' + location.href.substring(X, Y) + fn;

Finally, the location object is changed to the path value stored in the variable "link":

location.href = link;
}
</SCRIPT>

Here is the script without comments:


<SCRIPT Language="JScript">
function parser(fn) {
var X, Y, sl, a, ra, link;
ra = /:/;
a = location.href.search(ra);
if (a == 2)
X = 14;
else
X = 7;
sl = "\\";
Y = location.href.lastIndexOf(sl) + 1;
link = 'file:///' + location.href.substring(X, Y) + fn;
location.href = link;
}
</SCRIPT>

This is the syntax for the link:


<a onclick="parser('loremipsum.htm')" style="text-decoration: underline;
color: green; cursor: hand">Link to loremipsum.htm</a>

The target file name (in parentheses) is passed to the script as a string variable ("fn" in this example). This enables you to access the same script with multiple anchor tags so you can link to more than one file.

Notes



Back to topBack to top

Did you find this material useful? Gripes? Compliments? Suggestions for other articles? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.