views.py代码如下:
import os import execjs
from django.shortcuts import render,
def login(request):
project_path = os.path.dirname(os.path.abspath(__file__)) ile_path = project_path + r'\static\js\header.min.js' cddf = project_path + r'\node_modules' with open(ile_path, 'r', encoding='utf-8') as f: source = f.read() ctx = execjs.compile(source, cwd=cddf) ctx.call("add") return render(request, 'login.html')
urls.py代码如下:
from front import views
urlpatterns = [ path('login.html', views.login), ]
header.min.js代码如下:
const jsdom = require("jsdom"); const { JSDOM } = jsdom; const dom = new JSDOM(); window = dom.window; document = window.document; XMLHttpRequest = window.XMLHttpRequest;
function add() { var oDiv = document.getElementById('oDiv'); oDiv.style.display = "none"; }
注:oDiv是html中的一个a标签的id,html也引入了
<script type="text/javascript" src="../static/js/header.min.js"></script>
但是在执行过程中,一直出现
SyntaxError: Unexpected token ...