PDA

View Full Version : Writing annoying scripts.


Nikose Tyris
08-15-2007, 09:16 AM
over at the frontpage of www.wizards.com and www.vgcats.com they have a couple of randomly changing messages.

I really want to learn how to do that, but I can't seem to figure it out- and I can't find it online with google.

Any help would be appreciated.

Arlia Janet
08-15-2007, 10:15 AM
I figured this out from looking at the source of a few pages.

There are a few javascript functions that are called early in the page source:

rnd gets a random integer.

function rnd()
{
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number)
{
return Math.ceil(rnd()*number);
};

This assigns a image to load based on this random number:

function banner(){
var number=rand(5)-1



if (number == 0) {
document.write("<div align=left><a href=http://www.mozilla.org/products/firefox/ target=_blank>")
document.write("<img src=images/banner.gif alt=\"Link Destination\" border=0></a></div>")
}
if (number == 1) {
document.write("<div align=left><a href=http://www.mozilla.org/products/firefox/ target=_blank>")
document.write("<img src=images/banner2.gif alt=\"Link Destination\" border=0></a></div>")
}
if (number == 2) {
document.write("<div align=left><a href=http://www.mozilla.org/products/firefox/ target=_blank>")
document.write("<img src=images/banner3.png alt=\"Link Destination\" border=0></a></div>")
}
if (number == 3) {
document.write("<div align=left><a href=http://www.mozilla.org/products/firefox/ target=_blank>")
document.write("<img src=images/banner4.jpg alt=\"Link Destination\" border=0></a></div>")
}
if (number == 4) {
document.write("<div align=left><a href=http://www.mozilla.org/products/firefox/ target=_blank>")
document.write("<img src=images/banner5.gif alt=\"Link Destination\" border=0></a></div>")
}
}
a href can be dropped if these images shouldn't be links. You will need to scale this if-script depending on the number of random images/ lines of text you want.




This calls the function into the page:
<script language="JavaScript">
banner();
</script>

Nikose Tyris
08-15-2007, 12:05 PM
So if I wanted to do just basic words for something I'd need to put

<script language="JavaScript">

function rnd()
{
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number)
{
return Math.ceil(rnd()*number);
};
function banner(){
var number=rand(5)-1



if (number == 0) {
document.write("<div align=left>")
document.write("This is #0 wtf></div>")
}
if (number == 1) {
document.write("<div align=left>")
document.write("This is #1</div>")
}
if (number == 2) {
document.write("<div align=left>")
document.write("This is #2</div>")
}
if (number == 3) {
document.write("<div align=left>")
document.write("This is #3</div>")
}
if (number == 4) {
document.write("<div align=left>")
document.write("This is #4</div>")
}
}



</script>
? I'm still very new at coding anything, so I'm sorry if I have missed an obvious answer here.

Arlia Janet
08-15-2007, 12:30 PM
I've never coded javascript, it's just similar to Matlab which I had to use to survive college- and the coding is similar enough to infer what it's doing.

I know there is an easier way to do that- maybe a loop instead of if-statements, but I stole that code from a working website so I know that it should work. Try messing around with the code in Dreamweaver or NVU (http://www.nvu.com/) (free, almost identical version) to see how it works. There are a lot of Dreamweaver tutorials online that also work for NVU.