1

This should not at all be difficult but I can't get a simple JavaScript file included in a flat HTML page.

My JavaScript file is as follows:

<script>
  $(document).ready(function() {
  $('div').css('background', 'red');
 });
</script>

HTML code is as follows:

<html>
    <head>
        <script src="js/jquery-1.11.1.min.js"></script>
        <script src="js/init.js"></script>
    </head>
    <title>Song Index!</title>
    <body>
        <div>It's working!</div>
    </body>
</html>

I have confirmed both javascript files are in my js folder.

However, on Chrome I keep getting this error:

Uncaught SyntaxError: Unexpected token < init.js:1

Firefox renders a slightly different error:

SyntaxError: syntax error   <script> init.js (line 1)

I just don't get why it's not working.

2
  • 1
    Remove <script></script> tags from the javascript file Commented Nov 10, 2014 at 2:59
  • SLEEP DEPRAVATION - makes a developer do some dumb things :-) Thanks for all the answers. Commented Nov 10, 2014 at 15:42

3 Answers 3

3

Since you have script in a separate file the <script></script> is not required.

Your script is included within the html page using a script tag, so within your script file js/init.js there should not be the <script> and </script> parts

 $(document).ready(function() {
     $('div').css('background', 'red');
 });
Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript file should not containc tag. You javascript should be:

    $(document).ready(function() {
     $('div').css('background', 'red');
   });

Please move your tag in side tag. HTML code should be:

<html>
<head>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/init.js"></script>
    <title>Song Index!</title>
</head>
<body>
    <div>It's working!</div>
</body>

Comments

1

Simply remove the <script> tags from the external JS file:

    $(document).ready(function() {
        $('div').css('background', 'red');
    });

Comments

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.