如何让html在Django环境中从外部js文件调用javascript函数 [英] How to make html call a javascript function from an external js file, in Django environment
问题描述
我正在用 Django 开发一个应用程序.
我希望在用户登陆某个 HTML 页面时立即弹出一个警告窗口.
I am developing an app with Django.
I want an alert windows pops up as soon as the user lands on a certain HTML page.
弹出窗口的javascript函数叫做funprova
这个函数存储在一个名为 prova.js
的 js 文件中,在路径
The javascript function that makes the window pop up is called funprova
and this function is stored inside a js file called prova.js
, at the path
静态/js/prova.js
static/js/prova.js
我想让 HTML 调用这个函数.我该怎么做?
I want HTML call this function. How do I do that?
推荐答案
在外部 javascript 文件中的路径
In the external javascript file at path
静态/js/prova.js
static/js/prova.js
插入
function funprova() {
alert("Hello! I am an alert box from remote js file!!");
}
在需要弹出窗口的HTML页面中,在结束</body>
标签之前插入
In the HTML page that has to pop up the window, before of the closing </body>
tag, insert
{% load static %}
<script type="text/javascript" src={% static "js/prova.js" %}></script>
<script> funprova() </script>
注意:您必须在两个不同的脚本标签中调用 javascript,因为在前者中您调用的是一个存储在外部文件中的函数,而在后者中您调用的是一个已经存在的函数导入到 HTML 文件环境中.
Note: You have to call javascript in two differents script tags, because in the former you make the call to a function stored an external file, while in the latter you call a function that was already imported into the HTML file environment.
注意:最好将 {% load static %}
Django 标签插入 HTML 页面的最顶部,在 HTML 标签之前.
Note: It is better to insert the {% load static %}
Django tag at the very top of your HTML page, before the HTML tag.
这篇关于如何让html在Django环境中从外部js文件调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!