PDA

View Full Version : My timed background changer is not working



rsines
01-30-2011, 10:02 PM
Can someone tell me why this short script won't work?

<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
var number = rand(7);

if (number == 1) {
var bgcss = "theme1.css";
}
else if (number == 2) {
var bgcss = "theme2.css";
}
else if (number == 3) {
var bgcss = "theme3.css";
}
else if (number == 4) {
var bgcss = "theme4.css";
}
else if (number == 5) {
var bgcss = "theme5.css";
}
else if (number == 6) {
var bgcss = "theme6.css";
}
else if (number == 7) {
var bgcss = "theme7.css";
}

document.write('<link href="bgcss "'+ 'type=text/css rel=stylesheet>');
}
// -->
</script>

</head>

<body onload="JavaScript:timedRefresh(10000);">

Logic Ali
01-31-2011, 01:14 AM
Can someone tell me why this short script won't work?

document.write('<link href="bgcss "'+ 'type=text/css rel=stylesheet>');
All together now - "You can't call document.write after the page has loaded".

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Timed Style Switcher</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">

<link id="bgLink" href="theme1.css" type="text/css" rel="stylesheet" >

<script type="text/javascript">

function timedRefresh( timeoutPeriod )
{
var lnk = document.getElementById( 'bgLink' ), lastIdx = 1;

function f()
{
var idx;

while( ( idx = 1 + Math.floor( Math.random() * 7 ) ) == lastIdx )
;

lnk.href = 'theme' + idx +'.css' ;

lastIdx = idx;

document.title = lnk.href; /* Remove after test */
}

setInterval( f, timeoutPeriod );
}

</script>
</head>

<body onload="timedRefresh( 1000 );">
<p>
</body>
</html>

rsines
01-31-2011, 02:47 AM
That works great! Thank you very much!