Omnimaga

General Discussion => Technology and Development => Web Programming and Design => Topic started by: kalan_vod on January 29, 2011, 03:52:31 pm

Title: [JavaScript] Help With Code
Post by: kalan_vod on January 29, 2011, 03:52:31 pm
I have the code below, but it seems to be a bit slow. I have a broadband connection and the images it chooses are not big by any means..I am hoping to keep the JS not have to use PHP..It is a routine that randomly chooses a picture, any help would be great!

Code: [Select]
<script type="text/javascript">
if (document.getElementById) { window.onload = swap };
function swap() {
rndimg = new Array("newone", "blah", "bland", "blurp", "bookmark2", "boredom", "bunt", "directive", "floyd", "fusionbrick", "groundassult", "heartach2", "insight", "ism3", "purplenurple", "tinkr", "tunnel2");
x=(Math.floor(Math.random()*16));
randomimage=(rndimg[x]);
document.getElementById("background").style.backgroundImage = "url(images/"+ randomimage +".jpg)";
}
</script>
Title: Re: [JavaScript] Help With Code
Post by: Madskillz on January 29, 2011, 05:01:11 pm
I made a routine like that once a while ago let me see if I can find it. My knowledge of javascript is fairly low, but I managed to get mine to work. I don't know enough so I couldn't tell you which things to try and avoid etc.

edit: mine did virtually the same thing, except for I used quotes.

You shouldn't need to do this:
Code: [Select]
randomimage=(rndimg[x]);
Instead you can just do this:
Code: [Select]
document.getElementById("background").style.backgroundImage = "url(images/"+ rndimg[x] +".jpg)";
At least I think that should be fine...it is in the rest of the programming world.

Here's my code if you wanna look at it:
Code: [Select]
var quotes = new Array('"People are using the web to build things they have not built or written or drawn or communicated anywhere else." - Tim Berners-Lee','"One persons data is another persons program." - Guy L. Steele, Jr. ','"The computer was born to solve problems that did not exist before." - Bill Gates','"If you think your users are idiots, only idiots will use it." - Linus Torvalds','"Low-level programming is good for the programmers soul." - John Carmack');
var container = Math.floor((Math.random() * (quotes.length)));
document.writeln(quotes[container]);
Title: Re: [JavaScript] Help With Code
Post by: kalan_vod on January 29, 2011, 06:50:52 pm
Hey, thank you for your reply I will try that out. It worked, just seemed slow. Good to see you around here still buddy! Thanks for your code also
Title: Re: [JavaScript] Help With Code
Post by: Ikkerens on March 15, 2011, 02:00:11 pm
What you could do is build in a preloader, not displaying any content (or something like "Loading...") until the images are loaded.
I'l post some code if you're interested ;)