Problem Description:
JavaScript files are imported from outside the script tag, but it does not work, and the JS code is invalid
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Login</title>
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>
<form action="XXXXXX" method="post" name="login">
Username: <input type="text" name="username" id="username" /> <br>
Password: <input type="password" name="password" id="password" /> <br>
<input type="submit" name="login" id="login" value="登录"/>
<input type="button" name="logon" id="logon" value="注册" />
</form>
</body>
</html>
Cause analysis:
The execution order of HTML code is from top to bottom. The browser parses the HTML code from top to bottom. If JS is introduced into the head, the page tag may not be loaded during JS execution, resulting in the failure of JS to find the action object
Solution:
Just import an external JS file after the body
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Login</title>
</head>
<body>
<form action="http://42.192.60.64:8080/user" method="post" name="login">
Username: <input type="text" name="username" id="username" /> <br>
Password: <input type="password" name="password" id="password" /> <br>
<input type="submit" name="login" id="login" value="登录"/>
<input type="button" name="logon" id="logon" value="注册" />
</form>
</body>
<script src="js/main.js" type="text/javascript"></script>
</html>
Read More:
- Solution to some map files in JS folder after Vue packaging (remove the map. JS file)
- Solution to build error in Vue project (error in static/JS)/vendor.xxxxx.js from UglifyJs)
- JS bug Log Uncaught TypeError: Cannot read property ‘previoustSibling‘ of null
- [Solved] Nuxt Import qrcodejs2.js / QRCode.js Error: document is not defined
- [Solved] JS Error: Uncaught TypeError: Cannot set properties of null (setting ‘innerHTML‘)
- [Solved] Turf.js error: uncaught (in promise) error: the solution of invalid unit
- [Solved] Binding onclick event in JS: for loop: error uncaught typeerror: cannot set properties of undefined (setting ‘classname’)
- ‘webpack dev server’ is not an internal or external command, nor is it a runnable program or batch file. Solution: error in cnpm run dev:
- Vue project change directory run error:Multiple assets emit different content to the same filename index.html
- [Solved] webpack Package Error: ERROR in multi ./src/main.js ./dist/bundle.js Module not found: Error: Can‘t resolv
- JS uses onerror to automatically catch exceptions
- JS: How to Solve split, join, toString Use error
- [Solved] node.js request Error: Error: unable to verify the first certificate
- JS to determine whether the string contains a character
- Can’t resolve ‘ansi-html-community’ & Cannot find module ‘is-docker (vuex-persistedstate Installed)
- JS to find the last character of the string and remove it
- [vite] Failed to parse source for import analysis because the content contains invalid JS syntax.
- PDF.js load PDF Error: Message: failed to fetch
- How to Solve JS error: Unexpected end of JSON input,Unexpected token u in JSON at position 0
- JS getting ${pageContext.request.contextPath} Get the root path of the project