Reputation:
Can some one please let me know what is wrong with the following javascript. Its suppose to display the time and date but nothing but the HTML title is displayed. I am getting this from a javascript book im using to learn with and i think it may be to old now and things may have changed, or ive made a mistake.
<html>
<head><title>Date time</title></head>
<body>
<h1> Time and Date </h1>
<script LANGUAGE="JavaScript" type="textjavascript">
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<BR>");
document.write ("<b> UTC time: </b>" + utctime +);
document.write("Hello World!");
</script>
</body>
</html>
thanks for any help
Upvotes: 1
Views: 136
Reputation: 193271
Issue #1. It doesn't work because script block is not recognized as JS because of invalid type attribute:
type="textjavascript"
Should be type="text/javascript"
Issue #2. Once you fix this, make sure to fix this line too:
+ utctime +);
// ^ ---- remove this "+"
Issue #3. Throw away the book you read. It's totally outdated. Reason:
document.write
- it's used in very specific cases, your is not the one of those.LANGUAGE="JavaScript"
and type="text/javascript"
they are redundant.Finally, learn DOM methods like document.querySelector, insertAdjacentHTML, appendChild, createTextNode, createElement, etc., there are many of them.
You could rewrite your example in multiple ways, for example:
<html>
<head><title>Date time</title></head>
<body>
<h1> Time and Date </h1>
<div class="date"></div>
<script>
var now = new Date();
var localtime = now.toString();
var utctime = now.toGMTString();
var container = document.querySelector('.date');
container.innerHTML =
"<b> local time: </b>" + localtime + "<BR>" +
"<b> UTC time: </b>" + utctime;
document.body.appendChild(document.createTextNode("Hello World!"));
</script>
</body>
</html>
Upvotes: 2
Reputation: 18647
First change, <script type="text/javascript">, then
Just remove the '+' after utctime in document.write, it will work, that variable has no string to append ,so no '+' is needed.
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<BR>");
document.write ("<b> UTC time: </b>" + utctime );
document.write("Hello World!");
This will work.
Upvotes: 0
Reputation: 8354
change type="textjavascript"
to type="text/javascript"
and take out the extra +
in document.write ("<b> UTC time: </b>" + utctime +);
.
Upvotes: 0
Reputation: 4849
You have a redundant +
in the code. Also there is an error in the type
as well. Check this out.
<script type="text/javascript">
now = new Date();
localtime = now.toString();
utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<br>");
document.write ("<b> UTC time: </b>" + utctime);
document.write("Hello World!");
</script>
Upvotes: 0
Reputation: 1101
<html>
<head><title>Date time</title></head>
<body>
<h1> Time and Date </h1>
<script>
var now = new Date();
var localtime = now.toString();
var utctime = now.toGMTString();
document.write ("<b> local time: </b>" + localtime + "<BR>");
document.write ("<b> UTC time: </b>" + utctime);
document.write("Hello World!");
</script>
Upvotes: 0
Reputation: 4225
There are 2 errors in your code:
text/javascript
instead of textjavascript
"<b> UTC time: </b>" + utctime +
<-- Remove the trailing +
Upvotes: 1
Reputation: 2504
Just remove the + sign at the end of the following statement :
document.write ("<b> UTC time: </b>" + utctime +);
It should be :
document.write ("<b> UTC time: </b>" + utctime);
Upvotes: 0
Reputation: 476
You have issue in below line.
document.write ("<b> UTC time: </b>" + utctime +);
YOu have added extra "+" at last .
SO proper working code should be as below.
https://jsfiddle.net/tdc6dkn6/1/
Upvotes: 0