K
K
k1ng2017-01-20 14:45:34
JavaScript
k1ng, 2017-01-20 14:45:34

How to resolve Rails 5 turbolinks and document.write() conflict?

There is a Rails 5 project with turbolinks enabled.
You need to insert the pixel code for analytics on the pages. Pixel source code:

<script type='text/javascript'>
  var binom_link='http://tracker.domain/click.php?camp_id=xxx&key=zzzzzzzzzzzzzzzzz';
  document.write('<img src="'+binom_link+'&lp_type=pixel&'+window.location.search.substring(1)+'" style="display:none"/>');
</script>

But document.write() conflicts with turbolinks and breaks the DOM. How can this problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Melnikov, 2017-01-20
@mlnkv

var binom_link='http://tracker.domain/click.php?camp_id=xxx&key=zzzzzzzzzzzzzzzzz';
document.body.innerHTML += '<img src="'+binom_link+'&lp_type=pixel&'+window.location.search.substring(1)+'" style="display:none"/>'

// или
var img = document.sreateElement('img')
img.src = binom_link+'&lp_type=pixel&'+window.location.search.substring(1)
document.body.appendChild(img)

PS don't use `document.write()`, ever

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question