using this example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
You should include the system identifier (URL of the DTD), Quirks Mode is
triggered otherwise:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
[...]
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
You can safely omit the `language' attribute, then declare HTML 4.01 Strict.
var rnd = Math.ceil(Math.random()*10);
document.write(rnd);
if(rnd > 5){
document.write('<br><iframe src="
http://www.yahoo.com"></iframe>'); ^^
}else{
document.write('<br><iframe src="
http://www.google.com"></iframe>');
^^
ETAGO delimiters must be escaped in HTML `script' elements.
}
</SCRIPT>
</body>
</html>
This simple code writes an IFRAME element with a random src attribute
(Google , yahoo). The code works fine over IE, but have a strange
behavior on Fire Fox 1.5
It seems that the first src chosen is the dominate one
While I have been writing this, now I have read both your original posting
and your two followups. And the question occurred to me:
Do you really understand (the math of) random numbers?
The first branch should not be dominant. `rnd' can assume one of the
integer values 1 to 10 (because Math.random() returns IEEE-754 doubles x
from x = 0 to x < 1, and Math.ceil() returns the ceiling of its argument as
a IEEE-754 double). So the first condition is true iff `rnd' assumes one
of the values 6 to 10, that are 5 out of 10. Therefore, the statistical
probability of that event is 0.5 (or 50% or 1:2) as is the statistical
probability of the (opposite) event that `rnd' assumes one of the values 1
to 5.
However, probability is what can be observed only with a _large_ number of
repetitions of a random experiment. (This is the [Weak] Law of Large
Numbers that states, loosely speaking, that the relative frequency of an
event converges in a limit called the probability of that event as the
repetitions of the corresponding random experiment approach [positive]
infinity.)
So it is entirely possible (even though `rnd' is a computed random number
which differ from real random numbers), although not probable, that with
nine or nine hundred thousand repetitions of the experiment under the same
start conditions (in your case no-cache reloads, see below) the first
branch of the code is used each time, without violating this law.
The only reason why the probability of X=6..10 (X being the outcome of the
random experiment) could be greater than the probability of X=1..5 here is
that Math.random() could return 1 (which it should not according to
ECMAScript, but earlier Opera implementations are known for that). In that
case (X={1, 2, ..., 9, 10, ceil(1*10)}), the event X > 5 ({6, 7, 8, 9, 10,
10}) would occur with a probability of 6:11 and the opposite event (1 <= X
<= 5) would have to occur with a probability of only 5:11. However,
JavaScript (as implemented in Firefox) is not known to have this particular
bug in its Math.random() implementation.
That explained, you can simplify your little random experiment a lot:
var rnd = Math.random(); // still prone to the bug described above, though
// ...
if (rnd > 0.5)
{
// ...
}
else
{
// ...
}
Furthermore, you should not use consecutive calls of document.write.
Accumulate the strings to be written instead and write them once, e.g.
var rnd = Math.random(); // still prone to the bug described above, though
var a = [rnd, '<br>'];
if (rnd > 0.5){
a.push('<iframe src="
http://www.yahoo.com"><\/iframe>');
}
else
{
a.push('<iframe src="
http://www.google.com"><\/iframe>');
}
document.write(a.join(""));
and would not replace it self when reloading the page (F5).
Any workaround?
Ctrl+F5 or Ctrl+Shift+R, that overrides the browser cache.
PointedEars