The 3 Places You Can Use Scripts In MadCap Flare
- If you add your script to a Master Page file, it will run each time a new topic is loaded.
- If you add your script to a topic file, it will run when that particular topic is loaded.
- If you add your script to the skin file, it will run once, when the skin is first loaded.
Adding a Script to the Master Page
To add a script to a Master Page, you can enter the code manually by opening the Master Page file and using the Text Editor. If you want the Master Page to contain the script, enter the code inside <script> and </script> tags. If you are going to add code to use a function that is in an external script, you need to add the ‘call function’ part of the code in one set of <script> tags. You will then need to add the <script src> reference in a separate set of <script> tags. For example:
Note that the script in the body has to have the CDATA stuff around it. In the code in the screen shot above, the script is calling a fadeIn function with a time set to 3000ms. The code that defines what the fadeIn function does is in the jquery-1.7.2.min.js file that is referenced in the <head> section.
Alternatively, you can open the Master Page and use the XML editor instead – select the Insert menu and choose the Script option. If the Insert menu is unavailable, try adding a paragraph tag to the Master Page and then it should become available. You can delete the paragraph tag in the text editor later if needed.
If you use the Insert>Script technique, you will be presented with an Insert Script window. You can choose the language type and enter the code for your script there. The important thing to note here is that you will need to use the Insert Script window twice if you are referencing an external script. This is because the window isn’t designed to have the Script Code and Script Link settings used at the same time.
So the first time you can set the reference to the external script file – just select Insert > Script and then use the Script Link field to browse to the script file. I tend to keep all my script files in a ‘Scripts’ folder that I add to the ‘Resources’ folder. Select OK.
When you build your project, the browser will run the script whenever a new topic is loaded. You can try the script above in Flare 10+ and it will work, even if you don’t have the script file that is being referenced. This is because one of the other scripts that Flare uses also contains the fadeIn function (it is actually a jQuery script).
Adding a Script to a Topic
This works in the same way as described for the Master Page (above), but you use Insert Script on the topic file instead of the Master Page. The principles of how it works are the same, the only difference is that the script will only run when the browser loads that particular page.
Adding a Script to the Skin
Flare skin files cannot reference external scripts, so if you want the browser to run a script when the skin first loads, you will need to add all of the script in here.
If you have any good ideas for scripts (or even actual scripts that you are willing to share), please make a comment below.
CPD: 2 hours.