Reputation: 64
I am new to javascript and learning it from a book. I tried opening the webpage with the below HTML and js code. But the javascript part does not seem to be working.
Can someone please let me know on what is missing?
<head>
<title>Dynamic Page</title>
<script type="text/javascript">
var message = "Learning JavaScript will give your Web
page life!";
message += " Are you ready to learn? ";
var space = "...";
var position = 0;
function scroller() {
var newtext = message.substring(position, message.length) +
space + message.substring(0, position);
var td = document.getElementById("tabledata");
td.firstChild.nodeValue = newtext;
position++;
if (position > message.length) {
position = 0;
}
window.setTimeout(scroller, 200);
}
</script>
</head>
<body bgColor="darkgreen" onload="scroller();">
<table border="1">
<tr>
<td id="tabledata" bgcolor="white">message goes here</td>
</tr>
</table>
</body>
Upvotes: 0
Views: 115
Reputation: 15125
Your code was fine and it works, just minor formatting issues, you can't put break-lines in string literals. Fixed example below:
var message="Learning JavaScript will give your Web page life!";
message += " Are you ready to learn? ";
var space="...";
var position=0;
function scroller(){
var newtext = message.substring(position,message.length)+ space + message.substring(0,position);
var td = document.getElementById("tabledata");
td.firstChild.nodeValue = newtext;
position++;
if (position > message.length){position=0;}
window.setTimeout(scroller,200);
}
scroller();
<table border="1">
<tr>
<td id="tabledata" bgcolor="white">message goes here</td>
</tr>
</table>
Upvotes: 2
Reputation: 16908
The new line after
var message="Learning JavaScript will give your Web
is causing the problem, try to keep this sentence in one line or use the new ES6 multi-line string `` feature
var message="Learning JavaScript will give your Web page life!";
or
var message=`Learning JavaScript will give your
Web page life!`;
Upvotes: 1