[英]Flask not executing Javascript in static directory
Overview : I am using Ubuntu 14.04 in virtualbox, running the Python based Flask module to host a local portal with html, javascript, jquery and php elements. 概述 :我在virtualbox中使用Ubuntu 14.04,运行基于Python的Flask模块来托管具有html,javascript,jquery和php元素的本地门户。
Expected Outcome : I have a static JS file called test.js that I expect to be served when the template portal.html loads. 预期结果 :我有一个名为test.js的静态JS文件,我希望在加载portal.html模板时提供该文件。
Issue : The HTML page loads correctly, I receive a GET status 200 for test.js in the terminal, however the JS script does not execute (ie 'Twitter' is not present in the tags with id="screen_name"). 问题 :HTML页面正确加载,我在终端中收到test.js的GET状态200,但是JS脚本未执行(即ID =“ screen_name”的标签中不存在“ Twitter”)。 I checked localhost:5000/static/js/test.js and the script is present.
我检查了localhost:5000 / static / js / test.js并显示了脚本。 I tried including the exact same JS script directly in the HTML rather than as a static file and it worked fine.
我尝试将完全相同的JS脚本直接包含在HTML中,而不是作为静态文件,它运行良好。
Question : If my static JS script in being requested why is it not executing? 问题 :如果我的静态JS脚本在被请求时为什么不执行?
Directory Structure : 目录结构 :
portal
- portal_server.py
- templates
- portal.html
- static
- img
- css
- js
- test.js
Contents of test.js : test.js的内容 :
var x = document.getElementById('screen_name');
x.textContent = 'twitter';
Relevant element of HTML : HTML的相关元素 :
<h3 id="screen_name"></h3>
Script element of HTML : HTML的脚本元素 :
<script src="{{ url_for('static', filename='js/test.js') }}"></script>
One possible explanation: 一种可能的解释:
If your <script>
tag is in the <head>
of your document, it will execute before the body is loaded. 如果您的
<script>
标记位于文档的<head>
中,它将在加载正文之前执行。 In this situation, the element with Id "screen_name"
won't yet be loaded, and thus var x = document.getElementById('screen_name');
在这种情况下,标识为
"screen_name"
的元素将不会被加载,因此var x = document.getElementById('screen_name');
will not contain the result you expect. 将不包含您期望的结果。
If this is the case, try moving the <script>
tag to the end of your document. 在这种情况下,请尝试将
<script>
标记移至文档末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.