Load JS from kirby field with script tag and js condition

Hi all,

i need to load javascript (google tagmanager and other tracking code) with script tags from a backend field. In condition with a js bool var.

this is my code:

<script>
var allow=true
<?php

  foreach ($tags = site()->tags()->toStructure() as $tag){
    echo("if(allow===true){");
    echo($tag->code());
    echo("}");
  }

 ?>
</script>

This works with js code without script tags.

I need to get it work with tags in the backend.

Thx alot.

I would suggest adding a toggle to the stucture field for both the script tag and the allow option. That way you can choose to output the script tag or not and also turn each individual script on or off at will via PHP rather then JS

Hi @jimbobrjames,

thanks for your answer.
The js var is important cause it is the user decision in the frontend.

KR

In that case you can set a cookie with their choice using JavaScript and read it back with PHP to trigger the rest of the code accordingly.

yes, thats a good idea, thanks alot!