/    Sign up×
Community /Pin to ProfileBookmark

strange behave

The code below will not show all banners on my pc it skips 5 en 8.
Can somebody give me a hint on watt I do wrong. Chancing the pictures will not help

[code=html]<head>

<script type=”text/javascript”>

<!–
number_of_banners=8;

var sctr=0;
var halt=0;
var isn=new Array();
for (i=0;i<number_of_banners;i++){
isn[i]=new Image();
}

/* images all the same size (.gif of .jpg) */
isn[0].src=”../img/header/1.jpg”;
isn[1].src=”../img/header/2.jpg”;
isn[2].src=”../img/header/3.jpg”;
isn[3].src=”../img/header/4.jpg”;
isn[4].scr=”../img/header/5.jpg”;
isn[5].src=”../img/header/6.jpg”;
isn[6].src=”../img/header/7.jpg”;
isn[7].scr=”../img/header/8.jpg”;

function rotateIt(){
if (halt!=1){
sctr++;
if (sctr>number_of_banners-1){
sctr=0;
}
document.banner.src=isn[sctr].src;
setTimeout(“rotateIt()”,3000);
}
}

–>

</script>
</head>

<body>

<img SRC=” ” NAME=”banner” NOSAVE BORDER=0 height=160 width=745>

<script LANGUAGE=”JavaScript”>
<!–
sctr=0;
rotateIt();
–>
</script>

</body>

[/code]

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@tirnaMar 05.2011 — try something like this


[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"[URL]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd[/URL]">
<html xmlns="[URL]http://www.w3.org/1999/xhtml[/URL]">
<head>
<script type="text/javascript">
var picPaths = ['num1.jpg','num2.jpg','num3.jpg','num4.jpg','num5.jpg','num6.jpg','num7.jpg','num8.jpg'];
oPics = new Array();
for(i=0; i < picPaths.length; i++){
oPics[i] = new Image();
oPics[i].src = picPaths[i];
}
var curPic = 0;
function rotateImages(){
oImg.src = oPics[curPic].src;
curPic = (++curPic > oPics.length-1)? 0 : curPic;
setTimeout(rotateImages,2000);
}
window.onload=function(){
oImg = document.getElementById('picRotator');
rotateImages();
}
</script>
</head>
<body>
<div>
<img id="picRotator" src="" alt="" />
</div>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@keespijkauthorMar 05.2011 — Thanks tirna works perfect for me! ?

But I still do not know what is wrong with my script.?
Copy linkTweet thisAlerts:
@tirnaMar 05.2011 — You're not preloading your images correctly. Have a look at the difference between how I preload them and how you preload them.

I haven't looked for other errors.
Copy linkTweet thisAlerts:
@mcerritosMar 05.2011 — I copied this from a book:
[CODE]
<html>
<head>
<title>DOM Example</title>
<script language="javascript">
function changeBgColor(newColor){
if(document.body){
eval('document.body.bgColor="' + newColor +'"');
}
}
</script>

</head>

<body>
<form>Background color:
<select onchange="changeBgColor(this.options[this.selectedIndex].value);">
<option value="white">white</option>
<option value="red">red</option>
<option value="blue">blue</option>
<option value="green">green</option>
</select><br />


</form>
</body>
</html>
[/CODE]


But I cannot figure out why the following code doesn't work:

[CODE]
<html>
<head>
<title>DOM Example</title>
<script language="javascript">
function changeBgColor(form){
var colorme = form.colorme.value;
document.body.bgColor="'" + colorme + "'";
}
</script>

</head>

<body>
<form>Background color:
<select name="colorme" onchange="changeBgColor(form);">
<option value="white">white</option>
<option value="red">red</option>
<option value="blue">blue</option>
<option value="green">green</option>
<option value="orange">orange</option>
</select>
<br />


</form>
</body>
</html>
[/CODE]


Any ideas?
Copy linkTweet thisAlerts:
@tirnaMar 06.2011 — 
Any ideas?[/quote]


You are setting the bgColor to the string 'colorme' instead of the value contained in the variable colorme.

btw - bgColor is a long time deprecated now, and the way they use eval() is certainly not recommended nowadays so it is likely you are using an old book. Maybe consider getting a more up to date book.
Copy linkTweet thisAlerts:
@mcerritosMar 06.2011 — Is it possibe to change colorme so that it provides a value versus a string?
Copy linkTweet thisAlerts:
@cluefulMar 06.2011 — [CODE]document.body.bgColor = colorme;[/CODE]

It's better written thus:
[CODE]<script type = "text/javascript">

function changeBgColor( box )
{
document.body.style.backgroundColor = box.value;
}

</script>
...
<select name="colorme" onchange="changeBgColor( this );">
[/CODE]
Copy linkTweet thisAlerts:
@tirnaMar 06.2011 — Is it possibe to change colorme so that it provides a value versus a string?[/quote]

yes it is.

just use the variable name where you want its value instead of enclosing the variable name within quotes.

since bgColor is deprecated, the more correct way nowadays is

[CODE]
var colorme = 'red';

objectReference.style.backgroundColor = colorme;
[/CODE]


or if using functions, something like this
<i>
</i>&lt;select name="selXYZ" onchange="changeBgColor(this.value);"&gt;
...
...
&lt;script type="text/javascript"&gt;

function changeBgColor(newColor){
document.body.style.backgroundColor = newColor;
}

&lt;/script&gt;


</script>
Copy linkTweet thisAlerts:
@mcerritosMar 08.2011 — Thank you very much! You have been very helpful, tirna.
×

Success!

Help @keespijk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...