Ton*_*ony 45 javascript python external flask
我有一个名为showMap.html的HTML文件
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Map</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript" src="js/map.js"></script>
</head>
<body onload="showPosition()">
<div id="map_canvas" style="width:500px;height:500px;"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
另一个javascript文件map.js放在同一目录的js文件夹中.这个代码在加载html文件时工作正常.但是当我在服务器中运行它时这不起作用.我使用python flask框架返回结束编程.有趣的是,如果我扩展html文件中的javascript代码,同样会很好.唯一的问题是外部文件.
Mar*_*ers 95
将map.js
文件作为静态资源提供:
将文件移动到static/
包的子目录中
在Jinja2模板中为它生成一个静态URL,如下所示:
<script type="text/javascript"
src="{{ url_for('static', filename='map.js') }}"></script>
Run Code Online (Sandbox Code Playgroud)步骤1: 在项目根目录上创建名称为static的文件夹
步骤2: 在静态文件夹中添加静态文件
步骤3 添加模板
<script type="text/javascript" src="{{ url_for('static', filename = 'hello.js') }}"></script>
Run Code Online (Sandbox Code Playgroud)