简体   繁体   中英

Uncaught SyntaxError: Unexpected token when using localhost

I am new to javascript, need help. I have created a index.html and a app.js (both in same path), and have hosted the index.html as localhost.

When I run individual index.html I do get the hello alert, but when I run using localhost I get "Uncaught SyntaxError: Unexpected token <" error.

Below is the code that i used:

Index.html:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Show Javascript alert</title>
        <script type="text/javascript" src="app.js"></script>
    </head>
    <body>
    </body>
</html>

app.js:

function display_alert(){
    alert("Hello!");
}
window.onload = display_alert;

I think the Webserver is not serving your app.js File. Have you tried to open it in your Browser, to check if the Path is right?(eg http://localhost/app.js ).

Check if the Name of the File is Lowercase as well and if the Webserver has read Permissions on the File.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM