reed/turbolinks-compatibility

LiveZilla Compatability

AwakenTheJaken opened this issue · 0 comments

Dropping this information here in case someone else has trouble integrating LiveZilla into their Turbolinks 5 based Rails application.

Official Documentation:
<!-- livezilla.net PLACE SOMEWHERE IN BODY --><script type="text/javascript" id="lzdefsc" src="//yourdomain.com/script.php?id=lzdefsc" defer></script><!-- livezilla.net PLACE SOMEWHERE IN BODY -->

Turbolinks 5 Implementation:

<!-- livezilla.net PLACE SOMEWHERE IN HEAD --><script type="text/javascript" id="lzdefsc" src="//yourdomain.com/script.php?id=lzdefsc" defer></script><!-- livezilla.net PLACE SOMEWHERE IN HEAD -->

your.js

document.addEventListener("turbolinks:before-cache", function() {
  LiveZillaElements = findandgrabLiveZillaDomElements();
});

function findandgrabLiveZillaDomElements(){
  elements = []
  if(document.getElementById("lz_overlay_chat")){
    elements.push(document.getElementById("lz_overlay_chat"));
  }
  if(document.getElementById("lz_overlay_wm")){
    elements.push(document.getElementById("lz_overlay_wm"));
  }
  if(document.getElementById("lz_overlay_preview")){
    elements.push(document.getElementById("lz_overlay_preview"));
  }
  return elements
}

document.addEventListener('turbolinks:load', function() {
  if(LiveZillaElements.length){
    var body = document.getElementsByTagName("body")[0]
    for(var i=0;i<LiveZillaElements.length;i++){
      body.appendChild(LiveZillaElements[i]);
    }
  }
});