For some reason when I open this script with any browser nothing is displayed. It should bring up something like the following:
1
4
27
256
etc.
Any help would be appreciated.
<script type="text/javascript">
var m, n;
n = 10;
m = Math.floor(Math.random() * n + 1); //generates a random number between 1
and 10. Then converts value to a whole a number.
for (i = 1; i <= m; i++)
{
document.write(Math.pow(m,i));
document.write("<br>");
}
</script>
document.write() is not a dynamical method. Further more, is not a "concatenate" method. It will always write once and overwrite the result of the previous document.write(). U should use document.write only once.
Try
var txt=''
for (var i = 1; i <= m; i++)
{
txt += Math.pow(m,i)+'<br>';
}
document.write(txt)
digic25
03-18-2006, 04:04 PM
Thanks for the quick response...
But I'm still getting the same problem of a blank page.
Tom
I don't think so:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
var m, n;
n = 10;
m = Math.floor(Math.random() * n + 1);
var txt=''
for (var i = 1; i <= m; i++)
{
txt += Math.pow(m,i)+'<br>';
}
document.write(txt)
</script>
</head>
<body>
</body>
</html>
It works on any browser. Check whether your browser is javascript enabled or not
felgall
03-18-2006, 10:10 PM
document.write() is not a dynamical method. Further more, is not a "concatenate" method.
Actually it is a concatenate method. Using multiple document.write statements will work provided that they all run before the page finishes loading. The only problem arises when you try to run document.write statements after the page has loaded - that's when they will create a new page.
The other thing to watch for is that the code written by the document.write will be added after the end of the entire Javascript in some browsers rather than at the spot where the write statement appears. This can cause problems is using document.write to write script tags to write further Javascript that contains document.write statements.
Actually it is a concatenate method. Using multiple document.write statements will work provided that they all run before the page finishes loading. The only problem arises when you try to run document.write statements after the page has loaded - that's when they will create a new page.
The other thing to watch for is that the code written by the document.write will be added after the end of the entire Javascript in some browsers rather than at the spot where the write statement appears. This can cause problems is using document.write to write script tags to write further Javascript that contains document.write statements.
True, I have almost never used document.write(), I prefere innerHTML or DOM methods. But, anyway, the code I have posted must work. I don't sense why digic25 says it does not.