javascript document.write image error

只愿长相守 提交于 2019-12-12 05:53:18

问题


I am using Flask and pass variables (image_name) to javascript to get dynamic web content. I use those variables within document.write() to display a image file. Depending on the variable image_name the required .jpg file should be loaded.

This is my JS code:

document.write('<img class="img-circle profile_img" '
+ 'src="{{ url_for(' + "'static', filename='images/"
+ "'{{ image_name }}'" + ".jpg' )}}"
+ '" onerror="'
+ "this.onerror=null;this.src='../static/images/"
+ "'{{ image_name }}'"
+ ".jpg';"+'"'+"/>");

With image_name="max_mustermann" I want the output to be:

<img class="img-circle profile_img" src="{{ url_for('static', filename='images/max_mustermann.jpg' )}}" onerror="this.onerror=null;this.src='../static/images/max_mustermann.jpg';"/>

When I run Flask and try to load the page, my broswer is showing the following error.

jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'static'

来源:https://stackoverflow.com/questions/42553950/javascript-document-write-image-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!