Why is my additional script not loading on the checkout page?

Hello,

I have add that script in checkout page:

Τhe script does not appear on the thank you page. Only if I refresh the thank you page does the script appear

I also tried to put the {% for line_item in order.line_items %} in the whole code but it still didn’t work. Only when I refreshed the page did it bring results.

Ιf I remove the command {% for item in order.line_items %} and {% endfor %} then the script works normally but brings an empty result.

what is wrong with the code?

Best regards,
Alex