{% extends "base.html" %} {% block content %}

## Integrate Timeline using Javascript

Most of our users are happy to use the markup created in our simple authoring tool to publish their timeline. However, if you want to substantially customize the visual presentation of your timeline, or integrate it with other parts of your page, you will need to understand some more technical details.

There are three key things you need to include on your page to embed a timeline:

1. A `link` tag loading the Timeline CSS.
2. A `script` tag loading the Timeline javascript.
3. A second `script` tag which creates a Timeline.

Sometimes you'll add more things:

- configuration for custom fonts.
- a `link` tag to override some of TimelineJS's CSS.
- Javascript code to read configuration data from a Google Spreadsheet.

            <!-- 1 -->
            <link title="timeline-styles" rel="stylesheet"
                  href="https://cdn.knightlab.com/libs/timeline3/latest/css/timeline.css">

            <!-- 2 -->
            <script src="https://cdn.knightlab.com/libs/timeline3/latest/js/timeline.js"></script>

            <div id='timeline-embed' style="width: 100%; height: 600px"></div>

            <!-- 3 -->
            <script type="text/javascript">
                // The TL.Timeline constructor takes at least two arguments:
                // the id of the Timeline container (no '#'), and
                // the URL to your JSON data file or Google spreadsheet.
                // the id must refer to an element "above" this code,
                // and the element must have CSS styling to give it width and height
                // optionally, a third argument with configuration options can be passed.
                // See below for more about options.
                timeline = new TL.Timeline('timeline-embed',
                'https://docs.google.com/spreadsheets/d/1cWqQBZCkX9GpzFtxCWHoqFXCHg-ylTVUWlnrdYMzKUI/pubhtml');
            </script>

### Creating your own JSON

If you are retrieving JSON from a URL, you can simply use that URL as the second argument, just as if you are loading data from a Google spreadsheet. If instead you have something in your page which will put the data together, you can also pass a JSON object as the second argument.

Create the JSON according to our specifications and then change step 3 to look like this.

            <script type="text/javascript">
                // make_the_json() is some javascript function you've written
                // which creates the appropriate JSON configuration
                var timeline_json = make_the_json();
                window.timeline = new TL.Timeline('timeline-embed', timeline_json);
            </script>

### Configuring TimelineJS options

`TL.Timeline` offers an optional third parameter which you may use to pass in additional TimelineJS options. See our list of available options and configure like the example below.

            <script type="text/javascript">
                var additionalOptions = {
                  start_at_end: true,
                  default_bg_color: {r:0, g:0, b:0},
                  timenav_height: 250
                }

                timeline = new TL.Timeline('timeline-embed',
                'https://docs.google.com/spreadsheets/d/1cWqQBZCkX9GpzFtxCWHoqFXCHg-ylTVUWlnrdYMzKUI/pubhtml',
                additionalOptions);
            </script>

### Using custom fonts

Timeline offers several pre-selected font sets. When you use our simple tool, the fonts get included for you, but if you're using the methods on this page, and if you want to use anything but the default, you must use the `font` configuration option.

In the simplest case, you would choose from one of the font sets provided with TimelineJS. You can see what they look like in the "Optional settings" section of step three of the timeline authoring tool. It should be straightforward to compare the options shown there with these names to select the correct value.

- abril-droidsans
- amatic-andika
- bevan-pontanosans
- bitter-raleway
- clicker-garamond
- dancing-ledger
- default
- fjalla-average
- georgia-helvetica
- lustria-lato
- medula-lato
- oldstandard
- opensans-gentiumbook
- playfair-faunaone
- playfair
- pt
- roboto-megrim
- rufina-sintony
- ubuntu
- unicaone-vollkorn

Instead of one of the above values, you can create your own CSS file. For more details about that, see the Typography section of the "Overriding Timeline's Styles" documentation

Here are examples of how you might configure the font option.

            <script type="text/javascript">
                var additionalOptions = {
                    font: 'opensans-gentiumbook'
                }

                timeline = new TL.Timeline('timeline-embed',
                'https://docs.google.com/spreadsheets/d/1cWqQBZCkX9GpzFtxCWHoqFXCHg-ylTVUWlnrdYMzKUI/pubhtml',
                additionalOptions);
            </script>

            <!-- or, if you have created your own CSS file... -->
            <script type="text/javascript">
                var additionalOptions = {
                    font: '/css/my-custom-timeline-fonts.css'
                }

                timeline = new TL.Timeline('timeline-embed',
                'https://docs.google.com/spreadsheets/d/1cWqQBZCkX9GpzFtxCWHoqFXCHg-ylTVUWlnrdYMzKUI/pubhtml',
                additionalOptions);
            </script>

            <!-- another way, if you have created your own CSS file... -->
            <link rel="stylesheet" href="/css/my-custom-timeline-fonts.css">
            <script type="text/javascript">
                var additionalOptions = {
                    font: null
                }

                timeline = new TL.Timeline('timeline-embed',
                'https://docs.google.com/spreadsheets/d/1cWqQBZCkX9GpzFtxCWHoqFXCHg-ylTVUWlnrdYMzKUI/pubhtml',
                additionalOptions);
            </script>

### Loading Files Locally

If you choose not to load TimelineJS's JavaScript and CSS files from our CDN, you can also load the files locally. To do this, download our ZIP file here.

Unpack the ZIP and include it on your web server, and load the JS (either `timeline.js` or `timeline.min.js`) and CSS (`timeline.css`) files from it instead of from `cdn.knightlab.com`. If you are using the default fonts and English language, you only need those two files.

However, if you plan to use other languages and font sets, putting the entire contents of the ZIP on your server is necessary to fully support all of Timeline’s features.

If you include the TimelineJS javascript code in another file, using a bundler, "accelerator," or the like, you must help TimelineJS know where it can find font and language files. By default, the code assumes that they are served from a predictable location relative to the source. To do this, set the `script_path` option to a URL representing the directory where the core TimelineJS javascript files are stored. This should be a URL to which 'locale/es.json' could be added to load the Spanish (`es`) language translation files. Similarly, it should a URL which can be combined with a relative path such as `../css/fonts/font.default.css` could be added to locate the default font styles. The URL must end with a trailing slash.

If you do not set the `script_path` option, TimelineJS will basically work, but it will not be able to load translations and it won't be able to load the default font file, leaving it to inherit whatever CSS styles are already in place on the page.

Here is an example:

            <script type="text/javascript">
                var additionalOptions = {
                    script_path: '/timeline/js/',
                    language: 'es',
                    font: 'amatic-andika'
                }

                timeline = new TL.Timeline('timeline-embed',
                'https://docs.google.com/spreadsheets/d/1cWqQBZCkX9GpzFtxCWHoqFXCHg-ylTVUWlnrdYMzKUI/pubhtml',
                additionalOptions);
            </script>

As this example shows, it's fine to use a "relative" URL path for the `script_path` value if that works better for you.

{% endblock %}