Forum Moderators: open
i've got a little script which writes some HTML onto the page, together with an image and a link. and i've wrapped it up inside a function.
i have eight of these functions, all writing something different to the page.
they are called
function random1(){ blah blah blah }
function random2(){ blah blah blah }
function random3(){ blah blah blah } etc... up to function random8 my problem is, i need to find a way of choosing just two of them (it's got to be two different ones each time).
i know how you can generate a random number between 1 and 8, but how can you generate TWO random numbers between 1 and 8, making sure that each one is different?
it's driving me nuts
for (i = 1; i<=2; i++) {
var n = Math.floor(Math.random()*8); switch(n)
{
case 1:
random1();
break
case 2:
random2();
break
...
case 8:
random8();
}
}
And you can throw in a while statement to see if the new random variable is the same as the last one grabbed, and keep getting random numbers until they are different like Trace did in his(?) code.
[edited by: Gibble at 8:59 pm (utc) on Aug. 29, 2007]
function (eval("random" + Math.ceil(Math.random()*8) + "();")); i need it looking like:
function random5(); if i stick the eval function above in an alert box, it does come up with
random5();
i've got it working now, but i've come up against a javascript error in IE.
it doesn't really matter, though, because the script still fundtions perfectly well. but it keeps throwing up an 'Not implemented' error. Anyone know how to get rid of that?
Here is my (long-winded) script. I have cut some of it out to keep it short.
function pics(){function random1(){
var pic1=document.createElement('div');
pic1.setAttribute('class','pic');
pic1.setAttribute('className','pic');
pic1.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');
pic1.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";
var span1=document.createElement('span');
var pic1=document.createElement('a');
span1.appendChild(pic1);pic1.setAttribute('href','http://www.example.com');
pic1.appendChild(document.createTextNode('Anchor text'));
span1.appendChild(document.createTextNode('Some description text'));
pic1.appendChild(span1);
var scr=document.getElementById('pics');
scr.parentNode.insertBefore(pic1,scr);
}
function random2(){
var pic2=document.createElement('div');
pic2.setAttribute('class','pic');
pic2.setAttribute('className','pic');
pic2.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');
pic2.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";
var span2=document.createElement('span');
var pic2=document.createElement('a');
span2.appendChild(pic2);pic1.setAttribute('href','http://www.example.com');
pic2.appendChild(document.createTextNode('Anchor text'));
span2.appendChild(document.createTextNode('Some description text'));
pic2.appendChild(span2);
var scr=document.getElementById('pics');
scr.parentNode.insertBefore(pic2,scr);
}
.... etc... up to random4
addLoadListener(eval("random" + Math.ceil(Math.random()*4)));
} i know that the addLoadListener function works fine, so it's nothing to do with that.
You might be better of with something like this:
function create(r){
switch(r){
case 1:
var pic1=document.createElement('div');
pic1.setAttribute('class','pic');
pic1.setAttribute('className','pic');
pic1.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');
pic1.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";
var span1=document.createElement('span');
var pic1=document.createElement('a');
span1.appendChild(pic1);pic1.setAttribute('href','http://www.example.com');
pic1.appendChild(document.createTextNode('Anchor text'));
span1.appendChild(document.createTextNode('Some description text'));
pic1.appendChild(span1);
var scr=document.getElementById('pics');
scr.parentNode.insertBefore(pic1,scr);
break;
case 2:
var pic2=document.createElement('div');
pic2.setAttribute('class','pic');
pic2.setAttribute('className','pic');
pic2.setAttribute('style','background:url(/images/example.gif) no-repeat 0% 0%');
pic2.style.cssText="background:url(/images/example.gif) no-repeat 0% 0%;";
var span2=document.createElement('span');
var pic2=document.createElement('a');
span2.appendChild(pic2);pic1.setAttribute('href','http://www.example.com');
pic2.appendChild(document.createTextNode('Anchor text'));
span2.appendChild(document.createTextNode('Some description text'));
pic2.appendChild(span2);
var scr=document.getElementById('pics');
scr.parentNode.insertBefore(pic2,scr);
break;
.... etc... up to random4
}
addLoadListener(create(Math.ceil(Math.random()*4))); It avoids the use of eval which is normally quite slow.
Andrew
i've narrowed it down to these two lines (this is just example code, not the actual code):
var pic=document.createElement('img');
pic.setAttribute('href','http://www.example.com');
for some reason, IE7 doesn't like it when you set an href attribute on an <img> tag. when i remove the line, the 'Not implemented' error disappears, so it must be that.
Strangely, it doesn't mind if you set an href attribute on an <a> tag. what gives?