0

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?

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i forgot to paste the rest: in fact , it's something like you said. And i think it is because of twig translator? i have not found anything before it.
-1

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 :)

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.