Emmett said:
I have a simple jacascript that randomizes the background of the top
frame of my webpage
http://www.duke.edu/~efn. For some reason, my
Internet Explorer started blocking the background and displaying this
instead:
http://www.duke.edu/~efn/pic.jpg. This problem didn't used to
occur, and it doesn't happen on Firefox. Is there some way I can alter
the script of the top frame
http://www.duke.edu/~efn/top.html to
prevent this from happening? Thanks. -Emmett
Below is your script, and my comments on it:
<script language="javascript">
Use: <script type="text/javascript">
var bgNum = Math.round(Math.random() * 6);
Use: var bgNum = Math.floor(Math.random() * 6);
<url:
http://jibbering.com/faq/#FAQ4_22 />
bgSel = new Array(7);
bgSel[0] = "1.jpg";
bgSel[1] = "2.jpg";
bgSel[2] = "3.jpg";
bgSel[3] = "4.jpg";
bgSel[4] = "5.jpg";
bgSel[5] = "6.jpg";
bgSel[6] = "7.jpg";
Use:
var bgSel = [
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"5.jpg",
"6.jpg",
"7.jpg"
];
var base = bgSel[bgNum];
Note: You don't really need to assign this to another variable.
</script>
</head>
<script language="javascript">
document.write("<body background='" + base + "'>");
</script>
Note: you've got invalid markup here, because you've got a <script> tag
outside of <head></head> and <body></body>. And if Javascript is disabled,
the page gets no <body> tag at all.
Instead, you should use:
<head>
<style type="text/css">
body { background-image: url(default.jpg); }
</style>
<script type="text/javascript">
var bg = [
"1.jpg", "2.jpg", "3.jpg", "4.jpg",
"5.jpg", "6.jpg", "7.jpg"
];
document.write(
'<style type="text/css">',
'body { background-image: url(',
bg[Math.floor(Math.random() * bg.length)],
'); }',
'<\/style>'
);
</script>
</head>
<body>
Now you can expand the number of images by simply adding more elements to
the array (because we're using bg.length instead of a fixed number).
If the user has CSS disabled or over-ridden, they get no (or their choice
of) background-image.
If they have CSS enabled but JavaScript disabled they get the default
background-image (specified as default.jpg).
If they have CSS enabled and JavaScript enabled they get a random choice
of 1..7.jpg.
In no case do they get invalid markup or a page with no <body> tag.
Tested and working in IE 6.0.2800, Firefox 1.0PR, Netscape 4.78, Opera
6.05, Opera 7.54 and Mozilla 1.7.3.