There are two ways of adding third party scripts. Embedding is ideal for one-off scripts, e.g. widgets.js that is part of embedded tweets (see below). Adding global scripts is for scripts that should be loaded on every page.

<script async src="//" charset="utf-8"></script>
<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">The next version of Hydejack (v6.3.0) will allow embedding 3rd party scripts, like the one that comes with this tweet for example.</p>&mdash; Florian Klampfer (@qwtel) <a href="">June 3, 2017</a></blockquote>

Table of Contents


Hydejack supports embedding third party scripts directly inside markdown content. This will work in most cases, except when a script can not be loaded on a page more than once (this will occur when a user navigates to the same page twice).

NOTE: Adding “raw” script tags will make the page slow, unless they have the async or defer attribute set. For more see below.

Global scripts

If you have scripts that should be loaded on every page you can add them globally. Hydejack’s own script is loaded from _includes/scripts.html, but I’d recommend creating your own file called my-scripts.html (inside _includes).

You can put arbitrary HTML into my-scripts.html, but generally you’d want to add script tags. The same rules apply.

NOTE: Upgrading to a new version of Hydejack will overwrite scripts.html, so you have to go in and uncomment the line again. This is due to how Jekyll includes work.

async vs. defer vs. loadJSDeferred

I highly recommended setting the async or defer attribute on your external scripts (i.e. the ones that have a src attribute). Otherwise the entire page can’t finish loading until a separate HTTP request is completed, which can take a long time (this applies to the web in general, not just Hydejack).

Specific to Hydejack is the loadJSDeferred function, which is used to load Hydejack’s own scripts. It has various advantages which are detailed in the table below.

Downloadimmediatelyimmediatelyafter document load
Executionasapbefore document loadafter document load
Orderingnonepreserves ordervia callback nesting
SupportIE8+IE9+IE5+ (Hydejack only)

Using loadJSDeferred (Hydejack only)

Using loadJSDeferred is slightly more work than just adding defer to a script tag.

  loadJSDeferred('<script-src>', function () {
    // <callback code>

If you have scripts that depend on other scripts, you can nest calls, e.g.

  loadJSDeferred('<script-src-1>', function () {
    // <callback script 1>
    loadJSDeferred('<script-src-2>', function () {
      // <callback script 1 + 2>
      loadJSDeferred('<script-src-3>', function () {
        // <callback script 1 + 2 + 3>

Registering push state event listeners

When embedding scripts globally you might want to run some init code after each page load. However, the problem with push state-based page loads is that the load event won’t fire again. Luckily, Hydejack’s push state component exposes an event that you can listen to instead.

  document.getElementById('_yPushState').addEventListener('y-push-state-load', function() {
    // <your init code>

Note that the above code must only run once, so include it in your my-scripts.html.

Other events you can register on _yPushState include

Occurs when clicking a link
Animation fished and response has been parsed, ready to swap out the content.
The old content has been replaced with the new content.
The animation has finished playing.
Special case when animation is finished, but no response from server has arrived yet. This is also when the spinner will appear.
All embedded script tags have been inserted into the document and have finished loading.

Continue with Build

* Layout not included in free version

Powered by Hydejack v6.6.1