动态IFRAME问题 [英] Dynamic IFRAME problem

查看:59
本文介绍了动态IFRAME问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此示例:


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">

< html>

< head>

< title>测试页< / title>


< / head>


< body>


< SCRIPT LANGUAGE =" JavaScript" TYPE =" text / javascript">

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>'');

}

< / SCRIPT>


< / body>

< / html>


这个简单的代码用随机src属性写一个IFRAME元素

(谷歌,雅虎)。

代码在IE上运行良好,但在Fire Fox上有一个奇怪的行为

1.5


似乎所选择的第一个src是主导的,并且不会在重新加载页面时将
替换为自己。(/ 5)

任何解决方法?

using this example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Test Page</title>

</head>

<body>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
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>'');
}
</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 and would not
replace it self when reloading the page (F5).

Any workaround?

推荐答案

oops ......似乎IE正在做同样的场景io。


帮助?

oops ... It seems that IE is doing the same scenario.

help?


这是一个IE解决方法


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">

< html>

< head>

< title>测试页< / title>

< / head>

< body>

< SCRIPT LANGUAGE =" JavaScript" TYPE =" text / javascript">

var rnd = Math.ceil(Math.random()* 10);

document.write(rnd);

var f = document.createElement(" iframe");

f.id =" frm"

document.body.appendChild( f)

if(rnd> 5){

document.getElementById(" frm")。s​​rc =

" http: //www.google.com" ;;

} else {

document.getElementById(" frm")。s​​rc =" http://www.yahoo。 com" ;;

}

< / SCRIPT>

< / body>

< / html> ;


仍无法在Firefox上运行


帮助

here is an IE Workaround

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test Page</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
var rnd = Math.ceil(Math.random()*10);
document.write(rnd);
var f = document.createElement("iframe");
f.id = "frm"
document.body.appendChild(f)
if(rnd > 5){
document.getElementById("frm").src =
"http://www.google.com";
}else{
document.getElementById("frm").src = "http://www.yahoo.com";
}
</SCRIPT>
</body>
</html>

still dose not work on firefox

HELP


sh*************@gmail.com 写道:
sh*************@gmail.com wrote:
使用这个例子:

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">


你应该包括系统标识符(DTD的URL),否则怪癖模式是

触发:


<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"

" http://www.w3.org/TR/html4/loose.dtd" ;>

[...]
< SCRIPT LANGUAGE =" JavaScript" TYPE ="文本/ JavaScript的">


你可以安全地省略`language''属性,然后声明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分隔符必须以HTML脚本元素进行转义。

}
< / SCRIPT>

< / body>
< / html>

这个简单的代码用随机src属性写一个IFRAME元素
(谷歌,雅虎)。代码在IE上运行良好,但在Fire Fox 1.5上有一个奇怪的行为

似乎第一个选择的src是主导的


虽然我一直在写这篇文章,但现在我已经阅读了你原来的帖子

和你的两篇后续文章。问题出现在我身上:


你真的理解(数学)随机数吗?


第一个分支不应该占优势。 `rnd''可以假设其中一个

整数值为1到10(因为Math.random()从x = 0返回IEEE-754双倍x

到x< ; 1,Math.ceil()返回其参数的上限为

a IEEE-754 double)。所以第一个条件是真的,如果'rnd''假定一个值为6到10的值为b $ b,那就是10个中的5个。因此,统计

的概率是事件是0.5(或50%或1:2),因为rnd假设其中一个值(

)的(相反)事件的统计概率

然而,只有随机实验的重复次数大〜

才能观察到概率。 (这是大的[弱]定律

数字,松散地说,

事件的相对频率收敛于一个称为事件发生概率的极限作为

重复相应的随机实验方法[正]

无穷大。)


所以完全有可能(甚至虽然`rnd''是一个计算的随机数

,它与真实的随机数不同),虽然不太可能,但是在

下实验九九十万次重复相同的

启动条件(在你的情况下没有缓存重新加载,见下文)每次使用代码的第一个

分支,而不违反这个法律。 />

X = 6..10的概率(X是

随机实验的结果)的唯一原因可能大于X =的概率1..5这里是

,Math.random()可以返回1(它不应该根据

ECM AScript,但早期的Opera实现是众所周知的。在那个

的情况下(X = {1,2,...,9,10,ceil(1 * 10)}),事件X> 5({6,7,8,9,10,

10})将发生概率为6:11和相反的事件(1< = X

<= 5)必须以5:11的概率发生。但是,

JavaScript(在Firefox中实现)在Math.random()实现中不知道有这个特殊的

错误。


这解释了,你可以简化你的小随机实验:


var rnd = Math.random(); //仍然容易出现上述错误,但是

// ...

if(rnd> 0.5)

{

// ...

}

其他

{

// .. 。

}


此外,你不应该连续调用document.write。

收集要写的字符串并写一次,例如


var rnd = Math.random(); //仍然容易出现上述错误,但是


var a = [rnd,''< br>''];


if(rnd> 0.5){

a.push(''< iframe src =" http://www.yahoo.com">< \ / iframe>' ');

}

其他

{

a.push(''< iframe src =" ; http://www.google.com">< \ / iframe>'');

}


document.write(a .join(""));

并且在重新加载页面时不会自行替换它(F5)。

任何解决方法?
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或Ctrl + Shift + R,覆盖浏览器缓存。

PointedEars



Ctrl+F5 or Ctrl+Shift+R, that overrides the browser cache.
PointedEars


这篇关于动态IFRAME问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆