简体   繁体   English

烧瓶未在静态目录中执行Javascript

[英]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.

相关问题 Blazor Azure Static Web App not executing Javascript - Blazor Azure Static Web App not executing Javascript Flask,在Javascript中提供静态文件和/或文件夹 - Flask, serving Static File and or folder inside Javascript Flask应用程序位于外部文件中时未执行javascript - Flask app not executing javascript when it's in an external file 使用Flask从JavaScript代码执行python函数时出错 - Error in executing a python function from a javascript code using flask 从静态目录调用本地 Javascript 文件 - Call local Javascript files that from static directory flask无法读取静态路径并加载Javascript文件 - flask can not read static path and load Javascript files 没有用于 css 和 javascript 的静态文件夹的 Python Flask 蓝图 - Python Flask blueprint without static folder for css and javascript 有没有一种方法可以在我的/ public / javascripts目录中的javascript文件中执行嵌入式ruby? - Is there a way of executing embedded ruby in a javascript file in my /public/javascripts directory? 如何将 JavaScript 目录变量列表传递给 Python Flask - How to pass JavaScript list of directory variables to Python Flask 找不到django文件错误:当静态目录中的javascript文件试图访问静态目录中的另一个文件时? - django file not found error: when javascript file inside static directory trying to access another file in the static directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM