ferdinandfly
ferdinandfly

Reputation: 371

javascript in twig file do not execute

when I try to put some script in a twig file , and finally they do not execute, for example:

{% block myJavascript %}
<script type="text/javascript">
<!--
alert("test");
{% endblock %}  

in my index.html.twig
I see it in the generate html code but it just do not execute. Anyone has some idea?

Upvotes: 0

Views: 6169

Answers (2)

starterProgrammer
starterProgrammer

Reputation: 286

I had the same issue in opencart twig file, the script tag does not executed.

OLD CODE DOES NOT WORK

<script type="text/javascript">
    $(document).ready(function() {
    $('#button-confirm').trigger('click');
     var input = $(this).find('input');
        input.trigger('click');
    });
    alert('test');
</script>

NEW CODE

i just remove type="text/javascript" from script section like below code and it worked for me

<script>
    $(document).ready(function() {
    $('#button-confirm').trigger('click');
     var input = $(this).find('input');
        input.trigger('click');
    });
    alert('test');
</script>

I hope it will help :)

Upvotes: -1

Damien Roche
Damien Roche

Reputation: 176

That doesn't look right.

What about:

{% block myJavascript %}
<script type="text/javascript">
  alert("test");
</script>
{% endblock %}  

If js doesn't run, it can also mean there are errors triggered preceding this code, so make sure you check your browser's console.

For example, the first alert here will work, but the second will not:

<script>
  alert("this works");
  var = forced_error
  alert("this won't work");
</script>

Upvotes: 2

Related Questions