title: Prevent from executing previous JavaScript with turbolinks and highcharts //LazyHighCharts
body: <p>I'm using turbolinks 5 and rails 5 version and with newest Highcharts. T</p>
  
  <p>LazyHighCharts provides functionality for Turbolinks5 and wraps chart building javascript with </p>
  
  <p><code>(function() {
        document.addEventListener("turbolinks:load", function() {
          //code
          window.chart_my = new Highcharts.Chart(options);
        });
  })()</code></p>
  
  <p>But when I'm navigating away from page where graph was loaded previously with turbolink, I got error on newly loaded page. </p>
  
  <p><code>Uncaught Error: Highcharts error #13: www.highcharts.com/errors/13</code></p>
  
  <p>Looks like that all previous javascript which was loaded with <code>"turbolinks:load"</code> are reevaluating and causing highchart to throw error, because in new DOM there's no element to which that highchart graph is renderedTo.</p>
  
  <p>I tried to unbind <code>"turbolinks:load"</code> event from document, but it doesn't work. It reevaluates it anyway.</p>
  
  <p>Thought about solution to fork LazyHighCharts, to rewrite layout_helper, so javascript is outputed after dom element, but this would slow the page so the page flow would be</p>
  
  <p>loaded-dom (graph)
  evaluate js (sync)
  -->
  loaded-dom (graph)
  evaluate js (sync)
  --->
  loaded-dom (graph)
  evaluate js (sync)
  etc. and it would slow down page load i Guess.</p>
  
  <p>Maybe guys here have some solution to this problem.</p>