0

I am trying to call an external Javascript file as the Javascript for my HTML document. But I get this error: SyntaxError: Unexpected token "<" on line 1. So clearly it is acknowledging the file. But what I don't understand why it is throwing an error. I looked around and it seems no one else is has ever gotten this problem.

This is the HTML document:

<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
<script src="displaydate.js" type="text/javascript">
</script>
</head>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.<br>
<a href=http://www.google.com>google</a>
</p>


</body>
</html>

This is my Javascript:

<script type='text/javascript'>
function todaydate(){
var today_date= new Date();
var myyear=today_date.getYear();
var mymonth=today_date.getMonth()+1;
var mytoday=today_date.getDate();
alert(myyear+"//"+mymonth+"//"+mytoday);
}
</script>

2 Answers 2

5

You don't need the script-tags in the external file. Remove them and it will work.

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

1 Comment

Exactly right it worked at once now, thanks, I have been looking around for an answer for some time now.
4

A JavaScript file should contain only JavaScript.

Remove the HTML <script> tags from it.

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.