I'm trying to create an alert
in JavaScript when HTML page is loaded.
I linked the JavaScript file to the HTML file, but the alert doesn't appear.
What am I doing wrong?
The CSS file links successfully to the HTML file, and there are no errors on the developer tools on Chrome.
Code
alert("page is loaded");
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="js.js" type="text/Javascript"> <link rel="stylesheet" href="css.css" type="text/css"> </head> <body> <p id="firstParagraph">Hello world!</p> </body> </html>
Put your script inside <script>
tag and replace
<link rel="stylesheet" href="js.js" type="text/Javascript">
with
<script src="js.js"></script>
alert("page is loaded");
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css.css" type="text/css"> </head> <body> <p id="firstParagraph">Hello world!</p> <script src='js.js'></script> </body> </html>
Note: as a friendly advice, always locate your own javascript files right before closure tag of body , because then they will run after all context is being loaded .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.