html-customization-plugin-javascript.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <!DOCTYPE html
  2. SYSTEM "about:legacy-compat">
  3. <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="UTF-8"><meta name="copyright" content="(C) Copyright 2019"><meta name="DC.rights.owner" content="(C) Copyright 2019"><meta name="DC.type" content="task"><meta name="description" content="JavaScript code can be bundled in a custom plug-in and automatically inserted into the generated HTML pages to enable web analytics or dynamic content delivery."><meta name="DC.subject" content=", footer, require, head, transformations, HTML5, JavaScript, adding"><meta name="keywords" content=", footer, require, head, transformations, HTML5, JavaScript, adding"><meta name="DC.relation" scheme="URI" content="../topics/html-customization-plugins.html"><meta name="DC.relation" scheme="URI" content="../parameters/parameters-base-html.html"><meta name="DC.format" content="HTML5"><meta name="DC.identifier" content="custom-html-plugin-webfont"><link rel="stylesheet" type="text/css" href="../css/commonltr.css"><link rel="stylesheet" type="text/css" href="../css/dita-ot-doc.css"><title>Inserting JavaScript in generated HTML</title></head><body id="custom-html-plugin-webfont"><header role="banner"><div class="header">
  4. <p>DITA Open Toolkit</p>
  5. <hr>
  6. </div></header><nav role="toc"><ul><li><a href="../index.html">DITA Open Toolkit 3.3</a></li><li><a href="../release-notes/index.html">Release Notes</a></li><li><a href="../topics/installing-client.html">Installing DITA-OT</a></li><li><a href="../topics/alternative-input-formats.html">Authoring formats</a></li><li><a href="../topics/building-output.html">Building output</a></li><li><a href="../parameters/index.html">Setting parameters</a></li><li><a href="../topics/customizing.html">Customizing DITA-OT</a><ul><li><a href="../topics/html-customization.html">Customizing HTML</a><ul><li><a href="../topics/html-customization-parameters.html">Setting HTML parameters</a></li><li><a href="../topics/html-customization-properties-file.html">Using a properties file</a></li><li><a href="../topics/html-customization-plugins.html">Custom HTML plug-ins</a><ul><li><a href="../topics/html-customization-plugin-bundle-css.html">Bundling custom CSS</a></li><li><a href="../topics/html-customization-plugin-webfont.html">Embedding web fonts</a></li><li class="active"><a href="../topics/html-customization-plugin-javascript.html">Inserting JavaScript</a></li></ul></li></ul></li><li><a href="../topics/pdf-customization.html">Customizing PDF</a></li><li><a href="../topics/custom-plugins.html">Working with plug-ins</a></li><li><a href="../extension-points/plugin-extension-points.html">Extension points</a></li><li><a href="../topics/migration.html">Migrating customizations</a></li><li><a href="../topics/globalization.html">Globalizing DITA content</a></li><li><a href="../topics/rebuilding-docs.html">Rebuilding documentation</a></li></ul></li><li><a href="../topics/troubleshooting-overview.html">Troubleshooting</a></li><li><a href="../reference/index.html">Reference</a></li><li><a href="../topics/dita-and-dita-ot-resources.html">Resources</a></li></ul></nav><main role="main"><article role="article" aria-labelledby="ariaid-title1">
  7. <h1 class="title topictitle1" id="ariaid-title1">Inserting JavaScript in generated HTML</h1>
  8. <div class="body taskbody"><p class="shortdesc">JavaScript code can be bundled in a custom plug-in and automatically inserted into the generated HTML pages
  9. to enable web analytics or dynamic content delivery.</p>
  10. <section class="section context"><div class="tasklabel"><h2 class="sectiontitle tasklabel">About this task</h2></div>
  11. <p class="p">This scenario walks through the process of creating a very simple plug-in
  12. (<code class="ph codeph">com.example.html5-javascript</code>) that creates a new transformation type:
  13. <span class="keyword option">html5-javascript</span>. </p>
  14. <p class="p">The <span class="keyword option">html5-javascript</span> transformation includes a custom page footer file with a JavaScript
  15. tracking snippet and sets the <span class="keyword parmname">args.ftr</span> parameter to integrate the script content in the
  16. HTML5 <code class="keyword markupname xmlelement">&lt;footer&gt;</code> element of the generated pages.</p>
  17. <div class="note note note_note"><span class="note__title">Note:</span> This example inserts a tracking snippet for Google Analytics, but the basic approach is the same for other
  18. analytics platforms or similar use cases that require custom JavaScript.</div>
  19. </section>
  20. <section><div class="tasklabel"><h2 class="sectiontitle tasklabel">Procedure</h2></div><ol class="ol steps"><li class="li step stepexpand">
  21. <span class="ph cmd">In the <span class="ph filepath">plugins</span> directory, create a directory named
  22. <span class="ph filepath">com.example.html5-javascript</span>.</span>
  23. </li><li class="li step stepexpand">
  24. <span class="ph cmd">In the new <span class="ph filepath">com.example.html5-javascript</span> directory, create a plug-in configuration file
  25. (<span class="ph filepath">plugin.xml</span>) that declares the new <span class="keyword option">html5-javascript</span> transformation and
  26. its dependencies.</span>
  27. <div class="itemgroup stepxmp">
  28. <figure class="fig fignone"><figcaption><span class="fig--title-label">Figure 1. </span>Sample <span class="ph filepath">plugin.xml</span> file</figcaption>
  29. <pre class="pre codeblock language-xml normalize-space show-line-numbers show-whitespace"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  30. &lt;?xml-model href="dita-ot/plugin.rnc" type="application/relax-ng-compact-syntax"?&gt;
  31. &lt;plugin id="com.example.html5-javascript"&gt;
  32. &lt;require plugin="org.dita.html5"/&gt;
  33. &lt;transtype name="html5-javascript" extends="html5" desc="HTML5 with embedded JavaScript"/&gt;
  34. &lt;feature extension="ant.import" file="build_html5-javascript.xml"/&gt;
  35. &lt;/plugin&gt;</code></pre>
  36. </figure>
  37. <div class="note note note_note"><span class="note__title">Note:</span> This plug-in will extend the default HTML5 transformation, so the <code class="keyword markupname xmlelement">&lt;require&gt;</code>
  38. element explicitly defines <span class="ph filepath">org.dita.html5</span> as a dependency.</div>
  39. </div>
  40. </li><li class="li step stepexpand">
  41. <span class="ph cmd">In the <span class="ph filepath">com.example.html5-javascript</span> directory, create a subdirectory named
  42. <span class="ph filepath">include</span>.</span>
  43. </li><li class="li step stepexpand">
  44. <span class="ph cmd">In the new <span class="ph filepath">include</span> subdirectory, create a file named
  45. <span class="ph filepath">javascript.ftr.xml</span> with your custom JavaScript code.</span>
  46. <div class="itemgroup stepxmp">
  47. <figure class="fig fignone"><figcaption><span class="fig--title-label">Figure 2. </span>Sample <span class="ph filepath">javascript.ftr.xml</span> file</figcaption>
  48. <pre class="pre codeblock language-javascript normalize-space show-line-numbers show-whitespace"><code>&lt;div&gt;
  49. &lt;!-- Google Analytics --&gt;
  50. &lt;script&gt;
  51. console.log('Adding Google Analytics tracker');
  52. (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  53. (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  54. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  55. })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  56. ga('create', 'UA-XXXXX-Y', 'auto');
  57. ga('send', 'pageview');
  58. &lt;/script&gt;
  59. &lt;!-- End Google Analytics --&gt;
  60. &lt;/div&gt;</code></pre>
  61. </figure>
  62. <p class="p">The division wrapper will be discarded when generating HTML files, and the contents will be inserted into
  63. the <code class="keyword markupname xmlelement">&lt;footer&gt;</code> element of each page.</p>
  64. </div>
  65. </li><li class="li step stepexpand">
  66. <span class="ph cmd">In the <span class="ph filepath">com.example.html5-javascript</span> root directory, add an Ant script
  67. (<span class="ph filepath">build_html5-javascript.xml</span>) to define the transformation type and set the path to the
  68. JavaScript footer file created in the previous step.</span>
  69. <div class="itemgroup stepxmp">
  70. <figure class="fig fignone"><figcaption><span class="fig--title-label">Figure 3. </span>Sample build file: <span class="ph filepath">build_html5-javascript.xml</span></figcaption>
  71. <pre class="pre codeblock language-xml normalize-space show-line-numbers show-whitespace"><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  72. &lt;project&gt;
  73. &lt;target name="dita2html5-javascript"
  74. depends="dita2html5-javascript.init,
  75. dita2html5"/&gt;
  76. &lt;target name="dita2html5-javascript.init"&gt;
  77. &lt;property name="args.ftr"
  78. location="${dita.plugin.com.example.html5-javascript.dir}/include/javascript.ftr.xml"/&gt;
  79. &lt;/target&gt;
  80. &lt;/project&gt;</code></pre>
  81. </figure>
  82. </div>
  83. </li></ol></section>
  84. <section class="section result"><div class="tasklabel"><h2 class="sectiontitle tasklabel">Results</h2></div>
  85. <div class="note tip note_tip"><span class="note__title">Tip:</span> The files for this sample plug-in are included in the DITA-OT installation directory under
  86. <span class="ph filepath">docsrc/samples/plugins/com.example.html5-javascript/</span> and on <a class="xref" href="https://github.com/dita-ot/docs/tree/develop/samples/plugins/com.example.html5-javascript" target="_blank">GitHub</a>.</div>
  87. <p class="p">The plug-in directory has the following layout and files:</p>
  88. <pre class="pre codeblock"><code>com.example.html5-javascript
  89. ├── build_html5-javascript.xml
  90. ├── include
  91. │&nbsp;&nbsp; └── javascript.ftr.xml
  92. └── plugin.xml</code></pre>
  93. </section>
  94. <section class="section postreq"><div class="tasklabel"><h2 class="sectiontitle tasklabel">What to do next</h2></div>
  95. <ol class="ol">
  96. <li class="li">Run <span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/bin/<span class="keyword cmdname">dita</span></span>
  97. <span class="keyword parmname">--install</span> to install the plug-in and make the <span class="keyword option">html5-javascript</span>
  98. transformation available.</li>
  99. <li class="li">Build output with the new transformation type to verify that the plug-in works as intended.
  100. <pre class="pre codeblock"><code><span class="ph filepath"><var class="keyword varname">dita-ot-dir</var>/bin/<span class="keyword cmdname">dita</span></span> <span class="keyword parmname">--input</span>=<var class="keyword varname">my.ditamap</var> <span class="keyword parmname">--format</span>=<span class="keyword option">html5-javascript</span></code></pre></li>
  101. <li class="li">Open one of the generated HTML topic files in a modern web browser and check the JavaScript
  102. <span class="ph uicontrol">Console</span>. When the page is loaded, <samp class="ph msgph">Adding Google Analytics tracker</samp> will
  103. appear on the console to verify that the sample script is loaded.</li>
  104. <li class="li">Remove the <code class="ph codeph">console.log</code> debugging message from the sample JavaScript code, and replace the
  105. <code class="ph codeph">'UA-XXXXX-Y'</code> placeholder string with the tracking ID of the Google Analytics property you
  106. wish to track.</li>
  107. </ol>
  108. <div class="note tip note_tip"><span class="note__title">Tip:</span> This example places the JavaScript code in the page footer to ensure that page display is not
  109. delayed while the script is loaded. If your JavaScript code supports pre-loading and your application targets
  110. modern browsers that recognize the <code class="ph codeph">async</code> script attribute, you may prefer to insert the
  111. JavaScript snippet in the <code class="keyword markupname xmlelement">&lt;head&gt;</code> element of the generated HTML files using the
  112. <span class="keyword parmname">args.hdf</span> parameter intead.</div>
  113. </section>
  114. </div>
  115. <nav role="navigation" class="related-links"><div class="familylinks"><div class="parentlink"><strong>Parent topic:</strong> <a class="link" href="../topics/html-customization-plugins.html" title="For more complex customizations, you can create custom DITA-OT plug-ins that bundle custom fonts, JavaScript, and stylesheets; modify the HTML markup, or override other aspects of HTML processing.">Custom HTML plug-ins</a></div></div><div class="linklist relinfo relref"><strong>Related reference</strong><br><ul class="linklist"><li class="linklist"><a class="link" href="../parameters/parameters-base-html.html" title="Certain parameters apply to all HTML-based transformation types: HTML5, XHTML, HTML&nbsp;Help, Eclipse help, and TocJS.">HTML-based output parameters</a></li></ul></div></nav></article></main></body></html>