admin管理员组文章数量:1399251
Hello I think its a basic question or mistake i believe i want to ask here (my appologies for being ignorant on subject matter) but I want to run this code from on of the java's reference book (JavaScript: The Definitive Guide Book by David Flanagan) in a script tag in an html file. I sure it's probably a very little mistake, below is my code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-patible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>js book example1</title>
</head>
<body>
<p>test</p>
<script>
document.write("<h2>Table of Fibonacci Numbers</h2>");
for (i = 0, j = 1, k = 0. fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) {
document.write("Fibonacci(" + i + ") =" + fib);
document.write("<br>";)
}
</script>
</body>
</html>
Hello I think its a basic question or mistake i believe i want to ask here (my appologies for being ignorant on subject matter) but I want to run this code from on of the java's reference book (JavaScript: The Definitive Guide Book by David Flanagan) in a script tag in an html file. I sure it's probably a very little mistake, below is my code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-patible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>js book example1</title>
</head>
<body>
<p>test</p>
<script>
document.write("<h2>Table of Fibonacci Numbers</h2>");
for (i = 0, j = 1, k = 0. fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) {
document.write("Fibonacci(" + i + ") =" + fib);
document.write("<br>";)
}
</script>
</body>
</html>
Share
Improve this question
edited Feb 16, 2017 at 21:21
m87
4,5113 gold badges18 silver badges31 bronze badges
asked Feb 16, 2017 at 20:33
AbdulAbdul
211 gold badge1 silver badge2 bronze badges
2
- 1 Tip: use your browser's JavaScript console, it will report any script errors and anything else that requires your attention. – Dai Commented Feb 16, 2017 at 20:35
-
Also, avoid
document.write
- it's a slow and outmoded way of adding content to a webpage (and it won't work as you would expect in callback functions or event-handlers), use the DOM API instead (document.createElement
, etc). – Dai Commented Feb 16, 2017 at 20:36
2 Answers
Reset to default 2You have two mistypes:
k=0.
should bek=0,
document.write("<br>;")
should bedocument.write("<br>");
document.write("<h2>Table of Fibonacci Numbers</h2>");
for(i=0, j=1, k=0, fib=0; i<50; i++, fib=j+k, j=k, k=fib){
document.write("Fibonacci(" + i + ") =" +fib);
document.write("<br>");
}
A very easy to find out what's the problem (for the next time) is to open developer tools (press F12, if you use Chrome) and navigate to the console tab. There you would see the line where the problem is and you could possibly solve it in cases like these immediately.
I followed exactly the above approach, in order to find out what's wrong. I didn't even try to read the code :). The console tab had the following. If you notice at the rightmost part of the image you have the exact line, where the error emerged.
If you now click at the line (js:14), you will see the following:
By correcting this and start from the start you will notice the second error by following the same procedure.
You currently have two typos within your code that will throw off the syntax and thus cause your code not to work as expected :
// You had a period here instead of a ma (after "k=0"), which will cause
// the remainder of your for loop to not be properly parsed
for (i = 0, j = 1, k = 0, fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) { ... }
and :
// This was previously document.write("<br>";), note the transposed ";)" which should be
// ");"
document.write("<br>";)
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-patible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>js book example1</title>
</head>
<body>
<p>test</p>
<script>
document.write("<h2>Table of Fibonacci Numbers</h2>");
for (i = 0, j = 1, k = 0, fib = 0; i < 50; i++, fib = j + k, j = k, k = fib) {
document.write("Fibonacci(" + i + ") =" + fib);
document.write("<br>");
}
</script>
</body>
</html>
本文标签:
版权声明:本文标题:<script> tag is not working i am trying to run javascript script in script tag in html file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744120072a2591696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论