When you add your own JavaScript functions to your topics it is generally good practice to add the functions to the <head> section of your HTML page template and call the functions from within the topic with HTML code objects inserted in the topic. If you need different code functions in individual topics you can achieve this with editable variables.
Variables containing the actual code:
This assumes that you are going to insert the entire body of your JavaScript code in the <head> section of your HTML topic page template.
1.Define an HTML variable and paste your entire JavaScript code block in as the variable value, including the opening and closing <script> tags if appropriate.
You can insert as much code as you like but it cannot include line breaks so you must use semicolons to terminate all statements and then remove all line breaks in your editor with search and replace before copying and pasting.
2.Insert the variable in the appropriate position in your HTML topic page template (see Using HTML Templates for details). For example, suppose you want to add a block of code with a variable called <%JS_USERPOLL%>:
<meta http-equiv="Content-Style-Type" content="text/css" />
<link type="text/css" href="<%STYLESHEET%>" rel="stylesheet" />
<%JS_USERPOLL%>
</head>
3.To redefine the code for an individual topic select the topic in the Project Explorer and select the Topic Options tab on the left of the main editor window. Then click on +, select the <%JS_USERPOLL%> variable from the list and paste in the new code that you want to insert as the new variable value. That's it.
Of course, you could also use variables within your JavaScript code to replace just part of the code on a per-topic basis.
Variables containing a reference to a .js file:
This assumes your code is contained in an external .js file that you are referencing in your HTML topic page template.
1.Save the different versions of your JavaScript code in text files with the extension .js and add all the files to your Baggage Files to ensure that they will be exported correctly when you publish your project.
2.Define a plain-text variable and enter the name of the main JavaScript file (the one that will be used in most topics) as the variable value.
3.Add the include reference to your HTML topic page template (see Using HTML Templates for details) and use the variable instead of the name of the JavaScript file . For example, suppose the variable is called <%JS_FUNCS%>:
<meta http-equiv="Content-Style-Type" content="text/css" />
<link type="text/css" href="<%STYLESHEET%>" rel="stylesheet" />
<script type="text/javascript" src="<%JS_FUNCS%>"></script>
</head>
4.To select a different JavaScript file for an individual topic select the topic in the Project Explorer and select the Topic Options tab on the left of the main editor window. Then click on +, select the <%JS_FUNCS%> variable from the list and enter the name of the alternative JavaScript file as the new variable value. That's it. |