Context-Sensitive Help — An Easy Method Using Javascript
June 20th, 2007 | Posted in Technical Writing 14 Comments »
Context-sensitive help is usually perceived as being tedious or difficult to implement. However, if you’re writing help for a web application, this little javascript trick makes delivering context-sensitive help easy. Using this method, the developer doesn’t have to manually configure any of the URLs, and you as a tech writer have only a minimal amount of work as well. Each page in the application calls the right help topic for the right page. After seeing how easy it is to deliver context-sensitive help this way, I would never use any other method.
Conceptual Explanation
While easy to implement, it’s conceptually tricky to explain. Just so you don’t miss anything, here’s the concept of how it works. Each page in the application has a help button with the exact same code. When a user clicks the help button, the help button calls a javascript that says something like this:
- What is the name of the current page?
- OK, great, now change the extension of this page name to .htm.
- Call this exact same page from the help folder.
It does not matter what tool you are using for this method, as long as the application is a Web application (rather than a Winform application).
Writer’s Role
Writers must follow these steps to deliver the context-sensitive help:
- If you’re using a tool like RoboHelp or Flare (or any other tool, for that matter), match the help topic’s file names with the application page names that you’re associating the help topic with. For example, if the application page is called contact.aspx (look in the Web address to see the name), then you would rename your help topic’s file name contact.htm. If the application page is named config.aspx, then rename the help topic’s file name corresponding to this page config.htm.
- Remove each of your help files from any distinct subfolders. You can’t have some files inside folder A, some files inside folder B, others inside folder C, etc. All the files must be within the same folder.
- Let the developer know the path and folder of the published files.
Developer’s Role
- In the following javascript code, change the path in red to match the path of the published help folder, and insert this javascript into the header of each page (between the <header> tags).
<script>
function showHelp(){
//this is the name of the domain or the root url you want for the help link
var pagePrefix = “http://samplewebpath/acmeapplication/webhelp/index.htm#”
//this is the help extension that will replace whatever exists on the current page
var helpExtension = “.htm”
//this gets the current full path to the page
var pageName = window.location.pathname;
//this returns just the page name and its extension
pageName = pageName.substring(pageName.lastIndexOf(‘/’) + 1);
//this identifies just the page extension of the current page
var pageExtension = pageName.substring(pageName.lastIndexOf(‘.’));
//this replaces the current page name extension with the help extension
pageName = pagePrefix + pageName.replace(pageExtension, helpExtension)
// this shows you the link that will be opened
//alert(pageName);
//this is the popup script for the new window
myWindow = window.open(pageName, “tinyWindow”, ’scrollbars=yes,menubar=no,height=600,width=600,resizable=yes,toolbar=no,location=no,status=no’)
//this assures the window will come to the front of the current page
myWindow.focus()
}
</script> - In the body of your page, call the help topic like this:<a href=”javascript:showHelp()” mce_href=”javascript:showHelp()”><img src=”http://samplewebpath/acmeapplication/help.gif” border=”0″ width=”16″ height=”15″></a>
Obviously you need to replace the path in red with the path of your actual help button file.
That’s it. Now when the user clicks the help button, the page with the same file name in your help files will appear.
Externally Referencing the Javascript File
You can reference the javascript externally, rather than pasting the same code in each page’s header. To do that, rather than inserting that long script into the header of each page, add this instead:
<script src=”http://samplewebpath/acmeapplication/showhelp.js”> </script>
Open Notepad and insert the script, but remove the <script> tags from the beginning and end. Save the file with a .js extension.
Code the help button the same way as before.
Hiding the Table of Contents
Here’s a little trick I learned a while ago with RoboHelp.
What’s the difference between these two web paths?
- http://samplewebpath/acmeapplication/webhelp/index.htm#contact.htm
- http://samplewebpath/acmeapplication/webhelp/contact.htm
The first calls the topic Contact and also shows the navigation pane (table of contents).
The second shows the topic Contact without the navigation pane.
I like the navigation pane to show, so that’s why in the javascript code above, I wrote it like this:
var pagePrefix = “http://samplewebpath/acmeapplication/webhelp/index.htm#”
If you don’t want the navigation pane to show, you would write it like this:
var pagePrefix = “http://samplewebpath/acmeapplication/webhelp/“
Note: You must have a topic in your help file that has the file name index.htm (even if it’s a blank file). If you don’t (if it’s something like home.htm), you would use home.htm instead of index.htm in the javascript call.
Changing “Show” to “Show Table of Contents”
In RoboHelp, if you hide the table of contents, you’re left with a little link that says (quite nebulously) “show.” Show what? Where? To whom?
You can change this Show text. I don’t have RoboHelp open and in front of me, but if I remember correctly, go to File > Project Settings. Click the Advanced button. Click the LNG tab. Scroll until you see show=show. Select that and click the Edit button. Customize the text after the equals sign, like this: show=Show Table of Contents.
Credits
Special thanks to Pam Treme who showed me the context-sensitive help trick. And also special thanks to my sister for creating the Javascript code.
Twitter
Facebook












Hi Tom,
This is a great idea.
However, with our company’s web app, it won’t work because the web app uses a ton of AJAX that causes the content on the page to change without actually changing the URL.
But for a web application with distinct pages, this would be a great shortcut for getting it to work properly with minimal effort for everybody involved.
If you’re interested in Web-based context-sensitive Help, User First Services is a “don’t miss” site:
http://www.userfirst.net/tips1.html
However, as Rob Houser and Scott DeLoach’s “News” page says (http://www.userfirst.net/news.html), the userfirst.net URL will only be available at the end of 2007, so view the resources now before the link stops working.
The first presentation that I ever saw (quite a few years ago now) about context-sensitive links that work with Web apps was from User First Services, so they probably know more about this subject than most.
What I meant to say with my previous comment was that the userfirst.net URL will *only* be available until the end of 2007, so visit it earlier rather than later.
Paul,
You’re right about the limitations of the Javascript. Do you have any suggestions for getting around this problem?
Heidi,
Thanks for sending the link to that goldmine of a site. It took me a while before I really explored it — it has some excellent scripts there, including an equivalent to the one I posted above.
Under their title, “Opening a context-sensitive HTML-based Help topic from a Web page,” this is the code they use to call help by referencing the name of the page:
< script language="JavaScript">
function openHelp() {
helpurl = location.href;
begin=helpurl.lastIndexOf(‘/’);
begin = begin + 1;
end=helpurl.lastIndexOf(‘m’);
end=end + 1;
helpurl = “h_” + helpurl.substring(begin, end);
window.open(helpurl, ‘helpwin’,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=300,height=200′);
}
< /script>
open help window
I didn’t understand how they did the Embedded Help Demo sample. Do you know?
Thanks again for the link.
[...] JavaScript Tom Johnson has put together a brief tutorial on using JavaScript to make your online help context sensitive. Context-sensitive help is usually perceived as being tedious or difficult to implement. However, [...]
[...] Context-Sensitive Help — An Easy Method Using Javascript (http://www.idratherbewriting.com/2007/06/20/context-sensitive-help-an-easy-method-using-javascript/) [...]
How can we call help related to the context by pressing F1 key. Can you provide script for this.
How can we call help file related to a particular context by pressing F1 key. Can you provide script for this.
[...] Your page is on StumbleUpon [...]
Sorry, I’m not sure. It depends on the language. See this site: http://www.userfirst.net/tips1.html.
I know there’s an example there.
hi all,
I just wanted to let everyone know that I’ve moved the User First demos to my new website.
You can check out all of the context-sensitive help tips and the dynamic embedded help demo at http://www.clickstart.net/demos.
let me know if you have any questions–
.scott
Scott, thanks for the heads-up about the new site. I was actually thinking about the scripts on your old site last week, and how they were going away. I’m glad to hear you transferred the content to a new site.
And I’m also glad to see that you used WordPress to create the new site. Just curious, but will you be blogging from the site too, or are you just using WordPress as a traditional website?
THis was a great piece of information about CS Help for beginners. It was detailed, clear and very well told; my trainees are indebted to the article!
Thank you for the effort.