We're calling on all EU-based Mozillians with iOS or iPadOS devices to help us monitor Apple’s new browser choice screens. Join the effort to hold Big Tech to account!

為了改善您的使用體驗,本網站正在進行維護,部分功能暫時無法使用。若本站的文件無法解決您的問題,想要向社群發問的話,請到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 發問,我們的社群成員將很快會回覆您的疑問。

搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

了解更多

Javascript compatibility problem with disco background effect (works on IE but not FF)

  • 5 回覆
  • 9 有這個問題
  • 1 次檢視
  • 最近回覆由 cor-el

more options

This is the code:

r=255; g=255; b=255; flag=0; t=new Array; o=new Array; d=new Array;

function hex(a,c) { t[a]=Math.floor(c/16) o[a]=c%16 switch (t[a]) { case 10: t[a]='A'; break; case 11: t[a]='B'; break; case 12: t[a]='C'; break; case 13: t[a]='D'; break; case 14: t[a]='E'; break; case 15: t[a]='F'; break; default: break; } switch (o[a]) { case 10: o[a]='A'; break; case 11: o[a]='B'; break; case 12: o[a]='C'; break; case 13: o[a]='D'; break; case 14: o[a]='E'; break; case 15: o[a]='F'; break; default: break; } }

function ran(a,c) { if ((Math.random()>2/3||c==0)&&c<255) { c++ d[a]=2; } else { if ((Math.random()<=1/2||c==255)&&c>0) { c-- d[a]=1; } else d[a]=0; } return c } function do_it(a,c) { if ((d[a]==2&&c<255)||c==0) { c++ d[a]=2 } else if ((d[a]==1&&c>0)||c==255) { c--; d[a]=1; } if (a==3) { if (d[1]==0&&d[2]==0&&d[3]==0) flag=1 } return c } function disco() { if (flag==0) { r=ran(1, r); g=ran(2, g); b=ran(3, b); hex(1,r) hex(2,g) hex(3,b) document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3] flag=50 } else { r=do_it(1, r) g=do_it(2,g) b=do_it(3,b) hex(1,r) hex(2,g) hex(3,b) document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3] flag-- }

setTimeout('disco()',50) }

Can someone please tell me how to make this work in Firefox also, because I'm designing a website for which I'd love to put this as a background, but if I'm to do it, it needs to be compatible with all the major browsers.

This is the code: r=255; g=255; b=255; flag=0; t=new Array; o=new Array; d=new Array; function hex(a,c) { t[a]=Math.floor(c/16) o[a]=c%16 switch (t[a]) { case 10: t[a]='A'; break; case 11: t[a]='B'; break; case 12: t[a]='C'; break; case 13: t[a]='D'; break; case 14: t[a]='E'; break; case 15: t[a]='F'; break; default: break; } switch (o[a]) { case 10: o[a]='A'; break; case 11: o[a]='B'; break; case 12: o[a]='C'; break; case 13: o[a]='D'; break; case 14: o[a]='E'; break; case 15: o[a]='F'; break; default: break; } } function ran(a,c) { if ((Math.random()>2/3||c==0)&&c<255) { c++ d[a]=2; } else { if ((Math.random()<=1/2||c==255)&&c>0) { c-- d[a]=1; } else d[a]=0; } return c } function do_it(a,c) { if ((d[a]==2&&c<255)||c==0) { c++ d[a]=2 } else if ((d[a]==1&&c>0)||c==255) { c--; d[a]=1; } if (a==3) { if (d[1]==0&&d[2]==0&&d[3]==0) flag=1 } return c } function disco() { if (flag==0) { r=ran(1, r); g=ran(2, g); b=ran(3, b); hex(1,r) hex(2,g) hex(3,b) document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3] flag=50 } else { r=do_it(1, r) g=do_it(2,g) b=do_it(3,b) hex(1,r) hex(2,g) hex(3,b) document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3] flag-- } setTimeout('disco()',50) } Can someone please tell me how to make this work in Firefox also, because I'm designing a website for which I'd love to put this as a background, but if I'm to do it, it needs to be compatible with all the major browsers.

被選擇的解決方法

Seems to be working here. Be careful using % in c%16 -> c % 16. Otherwise %16 might get unescaped.

data:text/html;charset=utf-8,
<html>
<head>
<script>
var r=255,g=255,b=255,flag=0,t=new Array,o=new Array,d=new Array;

function hex(a,c) {
t[a]=Math.floor(c/16);o[a]=c % 16;
switch (t[a]) {
case 10:t[a]='A';break;
case 11:t[a]='B';break;
case 12:t[a]='C';break;
case 13:t[a]='D';break;
case 14:t[a]='E';break;
case 15:t[a]='F';break;
default:break;
}
switch (o[a]) {
case 10:o[a]='A';break;
case 11:o[a]='B';break;
case 12:o[a]='C';break;
case 13:o[a]='D';break;
case 14:o[a]='E';break;
case 15:o[a]='F';break;
default:break;
}
}

function ran(a,c) {
if ((Math.random()> 2/3 || c==0) && c<255){c++;d[a]=2;}
else{if ((Math.random() <= 1/2 || c==255) && c>0){c--;d[a]=1;}else d[a]=0;}
return c
}

function do_it(a,c) {
if ((d[a]==2 && c<255)|| c==0){c++;d[a]=2;}
else if ((d[a]==1 && c>0) || c==255){c--;d[a]=1;}
if (a==3) {if (d[1]==0 && d[2]==0 && d[3]==0)flag=1;}
return c
}

function disco() {
if (flag==0) {
r=ran(1, r);g=ran(2, g);b=ran(3, b);
hex(1,r);hex(2,g);hex(3,b);
document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3];
flag=50;
}
else {
r=do_it(1, r);g=do_it(2,g);b=do_it(3,b);
hex(1,r);hex(2,g);hex(3,b);
document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3];
flag--;
}
setTimeout('disco()',50);
}
</script>
</head>
<body style="height:100%;" onload="disco();">
</body>
</html>
從原來的回覆中察看解決方案 👍 1

所有回覆 (5)

more options

選擇的解決方法

Seems to be working here. Be careful using % in c%16 -> c % 16. Otherwise %16 might get unescaped.

data:text/html;charset=utf-8,
<html>
<head>
<script>
var r=255,g=255,b=255,flag=0,t=new Array,o=new Array,d=new Array;

function hex(a,c) {
t[a]=Math.floor(c/16);o[a]=c % 16;
switch (t[a]) {
case 10:t[a]='A';break;
case 11:t[a]='B';break;
case 12:t[a]='C';break;
case 13:t[a]='D';break;
case 14:t[a]='E';break;
case 15:t[a]='F';break;
default:break;
}
switch (o[a]) {
case 10:o[a]='A';break;
case 11:o[a]='B';break;
case 12:o[a]='C';break;
case 13:o[a]='D';break;
case 14:o[a]='E';break;
case 15:o[a]='F';break;
default:break;
}
}

function ran(a,c) {
if ((Math.random()> 2/3 || c==0) && c<255){c++;d[a]=2;}
else{if ((Math.random() <= 1/2 || c==255) && c>0){c--;d[a]=1;}else d[a]=0;}
return c
}

function do_it(a,c) {
if ((d[a]==2 && c<255)|| c==0){c++;d[a]=2;}
else if ((d[a]==1 && c>0) || c==255){c--;d[a]=1;}
if (a==3) {if (d[1]==0 && d[2]==0 && d[3]==0)flag=1;}
return c
}

function disco() {
if (flag==0) {
r=ran(1, r);g=ran(2, g);b=ran(3, b);
hex(1,r);hex(2,g);hex(3,b);
document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3];
flag=50;
}
else {
r=do_it(1, r);g=do_it(2,g);b=do_it(3,b);
hex(1,r);hex(2,g);hex(3,b);
document.body.style.background="#"+t[1]+o[1]+t[2]+o[2]+t[3]+o[3];
flag--;
}
setTimeout('disco()',50);
}
</script>
</head>
<body style="height:100%;" onload="disco();">
</body>
</html>

由 cor-el 於 修改

more options

Wow! Thanks a lot. Now it works in FF :-)

But only downside is it no longer works in IE. We all know IE doesn't follow W3C standards (transparency is such a pain in IE because it doesn't use rbga colors) and it's pretty much off its rocker, but this is just crazy.

Can you please, by any chance make your code run for both IE and FF or do I need to detect browser and run one of the two scripts according to the user's browser?

more options

I'm on Linux, so can't test it in IE. I would assume that only document.body.style.background could cause problems in IE. The rest is pretty standard. I only added some spaces and semicolons to make your code work as a data URI to test it.

A good place to ask questions and advice about web development is at the mozillaZine Web Development/Standards Evangelism forum.
The helpers at that forum are more knowledgeable about web development issues.
You need to register at the mozillaZine forum site in order to post at that forum.
See http://forums.mozillazine.org/viewforum.php?f=25

more options

Thanks a lot for your great help :-) I just used JavaScript to run your corrected script for FF users and the old one that ran on IE for IE users and so it's functioning just fine. I will look into the forums as you said for a more technical response regarding this. Thanks a million.