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;
user3370658
  • 81
  • 1
  • 2
  • 4
  • 1
    First of all I would change your `window.onload = display_alert;` to `window.onload = display_alert();` Take a look [here](http://stackoverflow.com/questions/3246928/in-javascript-does-it-make-a-difference-if-i-call-a-function-with-parentheses) for more information. – Grant Weiss Jul 23 '15 at 05:57
  • I works on my localhost. Seems like Your app.js has invicible or dirty symbols. here is original files: http://qush.it/nrcW – num8er Jul 23 '15 at 05:58
  • 1
    The error message doesn't seem related to the code in the question. – AD7six Jul 23 '15 at 05: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. – num8er Jul 23 '15 at 06: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. – Torben H. Jul 23 '15 at 06:21
  • @TorbenH. Yeah your right, I had them mixed up. Bad thing is I even referenced it and got it mixed up. – Grant Weiss Jul 23 '15 at 06:29

1 Answers1

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.

Torben H.
  • 160
  • 8
  • Hi Torben, when i open "http://localhost:8080/app.js" it opens the index.html. What could be the reason? – user3370658 Jul 23 '15 at 06:09
  • Which Webserver are you using? What is the operating System of your Server? – Torben H. Jul 23 '15 at 06:16
  • I am using node.js webserver, on Windows 7 Enterprise SP1. – user3370658 Jul 23 '15 at 06:18
  • 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 – Torben H. Jul 23 '15 at 06:23
  • How do I share node.js? its an application.. a freeware web server tool. – user3370658 Jul 23 '15 at 06:26
  • So i assume that you have not written the HTTP Script by yourself. which Server-Script are you using? express? – Torben H. Jul 23 '15 at 06:28
  • no, I havent written any HTTP script, what I have is only html and js file, and i have published the html as my local host... what do i need to do to make this work? – user3370658 Jul 23 '15 at 06:31
  • 1
    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? – Torben H. Jul 23 '15 at 09:08