0

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;
6
  • 1
    First of all I would change your window.onload = display_alert; to window.onload = display_alert(); Take a look here for more information. Commented Jul 23, 2015 at 5:57
  • I works on my localhost. Seems like Your app.js has invicible or dirty symbols. here is original files: qush.it/nrcW Commented Jul 23, 2015 at 5:58
  • 1
    The error message doesn't seem related to the code in the question. Commented Jul 23, 2015 at 5:59
  • maybe because chrome appends <![CDATA[ string to beginning of code so that's why You get error. but I don't think that this error could appear from code You've provided. Give more information. Commented Jul 23, 2015 at 6:02
  • @GrantWeiss When you use window.onload = display_alert(); then the function display_alert is called right away. When you are using it like in the Original Post, the function is excecuted, when the onload event occurs. This seems like the desired behaviour. Commented Jul 23, 2015 at 6:21

1 Answer 1

1

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?(e.g. 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.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi Torben, when i open "localhost:8080/app.js" it opens the index.html. What could be the reason?
Which Webserver are you using? What is the operating System of your Server?
I am using node.js webserver, on Windows 7 Enterprise SP1.
Than it should be an error in your HTTP-Route definitions inside your node.js script. I have to see your node.js code to determine the Problem
Node.js is not a Webserver by itself. you must have some kind of skript to make it server Files over http. Which command are you using to start the Server?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.