[英]Can't load Javascript file in HTML using Flask
I am trying to import a js file into my.html file and use the function in it but it doesn't seem to be working: script.js
我正在尝试将 js 文件导入 my.html 文件并在其中使用 function 但它似乎不起作用: script.js
function HidePassword() {
var x = document.getElementById("passwordInput");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
newclient.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title> Onboarding Portal - New Client</title>
</head>
<script type="text/javascript" src="../script.js"></script>
<body>
<a href="/" target="_self">
<button>Back</button>
</a>
<center>
Onboarding Portal
<br /><br /><br />
<form method="POST" action="/">
Password:
<input
type="password"
name="password"
value=""
id="passwordInput"
required
/><br /><br />
<input type="checkbox" onclick="HidePassword()" />Show Password
</center>
</body>
</html>
Am I missing something here?我在这里错过了什么吗? Should I not use script.js
like this?我不应该像这样使用script.js
吗?
Edit: here is the structure:编辑:这是结构:
Uncaught ReferenceError: HidePassword is not defined at HTMLInputElement.onclick
from flask import Flask, render_template, request
import onboardingscript
from onboardingscript import onboard_client, add_rec_to_existing_client
import json
from configparser import ConfigParser
import pandas as pd
from onboarding2 import onboard_client2
config_object = ConfigParser()
config_object.read("config.ini")
# create an instance of flask
app = Flask(__name__)
# Rename to the correct snake notation
# instantiate the app in one function -- COMPLETE
# Start adding config files to replace the credentials and other sensitive information
# add more error checking to your calls
# compartmentalize your functions
def create_app():
# # print(df)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/new-client')
def new_client():
return render_template('newclient.html')
@app.route('/existing-client')
def existing_client():
return render_template('existingclient.html')
@app.route('/', methods=['POST'])
def onboard_new_client():
# throw all variables captured by form and throw it into script, and run it
#create a large function that takes in the calls and have it run in sequential order for this
onboarding_data = {
'clientFirstName': request.form['clientFirstName'],
'clientLastName': request.form['clientLastName'],
'clientId': request.form['clientId'],
'clientEmail': request.form['clientEmail'],
'envOption': request.form['env']
}
result = onboard_client2(onboarding_data)
# result = onboard_client(onboarding_data)
if (result == True):
return render_template('confirmation.html')
else:
return render_template('failure.html')
@app.route('/a', methods=['POST'])
def onboard_existing_client():
onboarding_data = {
'clientEmail': request.form['clientEmail'],
'envOption': request.form['env']
}
result = add_rec_to_existing_client(onboarding_data)
if (result == True):
return render_template('confirmation.html')
else:
return render_template('failure.html')
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)
EDIT: I am thinking about just manually typing it in <script>
blocks.编辑:我正在考虑在<script>
块中手动输入它。 This was just me trying to save space/time by having it in one.js file because this function is used in two places.这只是我试图通过将它放在 one.js 文件中来节省空间/时间,因为这个 function 在两个地方使用。
Create a directory named static
at the same level as templates
and put your script file in it.在templates
的同一级别创建一个名为static
的目录,并将您的脚本文件放入其中。 You'll be able to load it like so:您将能够像这样加载它:
<script type="text/javascript" src="/static/script.js"></script>
Note that this is also valid for other files such as images or css files.请注意,这也适用于其他文件,例如图像或 css 文件。
NOTE: The current position of your <script>
import is not correct.注意: <script>
导入的当前 position 不正确。 Either put it inside your <head>
or inside <body>
.要么把它放在你的<head>
里面,要么放在<body>
里面。 Even tho it will load the script file on most modern browsers, this is not a valid HTML syntax and will get you errors with the W3C validator.即使它会在大多数现代浏览器上加载脚本文件,这也不是有效的 HTML 语法,并且会在 W3C 验证器中出现错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.