Welcome to Dream.In.Code
Become an Expert!

Join 149,532 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,522 people online right now. Registration is fast and FREE... Join Now!




'Random' Image Code...

 
Reply to this topicStart new topic

'Random' Image Code..., IE works, Firefox doesnt...

Squirrel
30 May, 2007 - 09:45 PM
Post #1

D.I.C Head
**

Joined: 6 Feb, 2007
Posts: 61


My Contributions
I have this code on my page...
CODE

<script language="javascript" type="text/javascript">
            <!-- Begin
            var rndprog=0;
            var rndvid=0;
            var rndpic=0;
            
            rndprog = Math.floor(2*Math.random()+1);    
            if (rndprog==1)
            { randomprogram.src='images/thumb_insultgenerator.jpg' };
            if (rndprog==2)
            { randomprogram.src='images/thumb_income.jpg' };
            
            rndvid = Math.floor(13*Math.random()+1);
            if (rndvid==1)
            { randomvideo.src='images/thumb_aimeesnowangel.jpg' };
            if (rndvid==2)
            { randomvideo.src='images/thumb_asuwheelie.jpg' };
            if (rndvid==3)
            { randomvideo.src='images/thumb_butane.jpg' };
            if (rndvid==4)
            { randomvideo.src='images/thumb_caralarm.jpg' };
            if (rndvid==5)
            { randomvideo.src='images/thumb_chikfila.jpg' };
            if (rndvid==6)
            { randomvideo.src='images/thumb_christmas.jpg' };
            if (rndvid==7)
            { randomvideo.src='images/thumb_halloween.jpg' };
            if (rndvid==8)
            { randomvideo.src='images/thumb_jhop.jpg' };
            if (rndvid==9)
            { randomvideo.src='images/thumb_largestyet.jpg' };
            if (rndvid==10)
            { randomvideo.src='images/thumb_monitor.jpg' };
            if (rndvid==11)
            { randomvideo.src='images/thumb_snowmen.jpg' };
            if (rndvid==12)
            { randomvideo.src='images/thumb_tv.jpg' };
            if (rndvid==13)
            { randomvideo.src='images/thumb_wheelie.jpg' };

            rndpic = Math.floor(12*Math.random()+1);
            if (rndvid==1)
            { randompicture.src='pictures/thumbs/thumb_aimeereturns.jpg' };
            if (rndvid==2)
            { randompicture.src='pictures/thumbs/thumb_brianandjackie.jpg' };
            if (rndvid==3)
            { randompicture.src='pictures/thumbs/thumb_jackiewithdetonator.jpg' };
            if (rndvid==4)
            { randompicture.src='pictures/thumbs/thumb_lighter.jpg' };
            if (rndvid==5)
            { randompicture.src='pictures/thumbs/thumb_lightingbomb.jpg' };
            if (rndvid==6)
            { randompicture.src='pictures/thumbs/thumb_locknload.jpg' };
            if (rndvid==7)
            { randompicture.src='pictures/thumbs/thumb_lolshoe.jpg' };
            if (rndvid==8)
            { randompicture.src='pictures/thumbs/thumb_naughtyprinter.jpg' };
            if (rndvid==9)
            { randompicture.src='pictures/thumbs/thumb_nickandtree.jpg' };
            if (rndvid==10)
            { randompicture.src='pictures/thumbs/thumb_nickheadrub.jpg' };
            if (rndvid==11)
            { randompicture.src='pictures/thumbs/thumb_rockyourfaceoff.jpg' };
            if (rndvid==12)
            { randompicture.src='pictures/thumbs/thumb_speck.jpg' };
            if (rndvid==13)
            { randompicture.src='pictures/thumbs/thumb_speckshand.jpg' };
            if (rndvid==14)
            { randompicture.src='pictures/thumbs/thumb_speckshouse.jpg' };
            if (rndvid==15)
            { randompicture.src='pictures/thumbs/thumb_trainguestbook.jpg' };
            if (rndvid==16)
            { randompicture.src='pictures/thumbs/thumb_itsabomb.jpg' };

            // End -->
            </script>

I don't do Java usually so maybe you can tell me why it works in IE but fails to work in FireFox :-/ Personally I'm not a fan of firefox because of these quarks but maybe someone can redeem it?

This post has been edited by skyhawk133: 31 May, 2007 - 07:44 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: 'Random' Image Code...
30 May, 2007 - 11:54 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE
I don't do Java usually so maybe you can tell me why it works in IE but fails to work in FireFox :-/ Personally I'm not a fan of firefox because of these quarks but maybe someone can redeem it?


This is Java??? Well I think this is JavaScript which is not the same thing with Java (not even close). So next time post in the HTML/CSS/JavaScript forum.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: 'Random' Image Code...
31 May, 2007 - 12:22 AM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Moved to JavaScript thread.
User is offlineProfile CardPM
+Quote Post

Squirrel
RE: 'Random' Image Code...
31 May, 2007 - 04:43 PM
Post #4

D.I.C Head
**

Joined: 6 Feb, 2007
Posts: 61


My Contributions
CODE
<script language="javascript" type="text/javascript">
            <!-- Begin
            var rndprog=0;
            rndprog = Math.floor(2*Math.random()+1);    
            switch(rndprog)
            {
            case 1:
                document.write("\"Insult Generator\"")
                randomprogram.src='images/thumb_insultgenerator.jpg'
                break
            case 2:    
                document.write("\"Income Calculator\"")
                randomprogram.src='images/thumb_income.jpg'
                break
            }
            // End -->
            </script>

Thats a problem in both this time... any suggestions?

This post has been edited by Squirrel: 31 May, 2007 - 05:25 PM
User is offlineProfile CardPM
+Quote Post

girasquid
RE: 'Random' Image Code...
31 May, 2007 - 07:42 PM
Post #5

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
You seem to be missing some semi-colons inside your JS. I don't think that's the only issue, but that could be part of it.
User is offlineProfile CardPM
+Quote Post

Squirrel
RE: 'Random' Image Code...
31 May, 2007 - 10:57 PM
Post #6

D.I.C Head
**

Joined: 6 Feb, 2007
Posts: 61


My Contributions
QUOTE(girasquid @ 31 May, 2007 - 08:42 PM) *

You seem to be missing some semi-colons inside your JS. I don't think that's the only issue, but that could be part of it.


I assumed so, but i dont know enough about JavaScript to know where/when
User is offlineProfile CardPM
+Quote Post

Arbitrator
RE: 'Random' Image Code...
1 Jun, 2007 - 06:33 AM
Post #7

D.I.C Regular
Group Icon

Joined: 26 Jan, 2005
Posts: 492



Thanked: 1 times
My Contributions
Your code exhibits some poor practice.

Poor Practice: HTML
  • Don’t use the language attribute.
  • Don’t use SGML comments within the script element. In other words, don’t use <!-- or -->.
  • Just an FYI: The media type text/javascript is, technically, outdated. Internet Explorer doesn’t support application/javascript or application/ecmascript though, so you’ll end up using the incorrect media type anyway.

Poor Practice: JavaScript
  • Don’t put semicolons after the Right Curly Bracket (}) of an if statement.
  • Do close expressions with a semicolon. Unfortunately, I’m not sure if “expressions” is the correct term. To make things clearer: document.write();, break;, and lines where something is assigned using the assignment operator (the Equals Sign (=)) are examples of things that should end with a semicolon.
  • Don’t use the document.write(); method. Use DOM methods to write content to a document instead.
  • Use DOM methods to create new elements. For example, use var image = document.createElement("img");.
  • Use generic DOM methods to declare new attributes. For example, use image.setAttribute("src", "image.png");.
  • Don’t use if for every statement if they’re related; use if for the first statement and else if for subsequent occurrences. If you do otherwise (what you are currently doing), all of the subsequent if statements are checked even after a match has been found.

Errors: JavaScript
  • The randomprogram variable is not declared and does not refer to an img element.

You might want to learn how Math.random() works. Try reading the article Make JavaScript Math.random() Useful. After reading it, you should be able to tell that your first and fourth use of the Math.random() method only generates integers between 2 and 3 inclusive; the second only generates integers between 13 and 25 inclusive; and the third between 12 and 23 inclusive. This is clearly not what you wanted, since some of the values that you’re trying to match fall out of those ranges.

Finally, I wrote an example random image generator seven days ago. It demonstrates the correct way to write such a script (compatible with Firefox, Opera, et al) and, at the same time, a second, semi‐correct script demonstrates a method that’s compatible with Internet Explorer’s lack of support. See Demo CF114985.

You probably won’t understand some of the things. I would start learning the stuff shown at W3C DOM Compatibility - Core. To see how I served code separately to Internet Explorer, read CSS - Conditional Comments.

If you have any more questions after all of that, do some research or ask around.
User is offlineProfile CardPM
+Quote Post

Squirrel
RE: 'Random' Image Code...
2 Jun, 2007 - 12:30 PM
Post #8

D.I.C Head
**

Joined: 6 Feb, 2007
Posts: 61


My Contributions
I understand actually what they are supposed to do, but I still cant get this line of code to work in IE. The image tag in question is named "randomprogram" so it should adjust it accordingly but it wont. Any ideas?

CODE
var randomprogram = document.createElement("randomprogram");
            var rndprog=0;
            rndprog = Math.floor(2*Math.random()+1);
            
            switch(rndprog)
            {
            case 1:
                randomprogram.setAttribute("src", "images/thumb_insultgenerator.jpg");
                break;    
            case 2:    
                randomprogram.setAttribute("src", "images/thumb_income.jpg");
                break;
            }


User is offlineProfile CardPM
+Quote Post

Arbitrator
RE: 'Random' Image Code...
2 Jun, 2007 - 10:05 PM
Post #9

D.I.C Regular
Group Icon

Joined: 26 Jan, 2005
Posts: 492



Thanked: 1 times
My Contributions
QUOTE(Arbitrator @ Posted 1 Jun, 2007 - 09:33 AM)
After reading it, you should be able to tell that your first and fourth use of the Math.random() method only generates integers between 2 and 3 inclusive; the second only generates integers between 13 and 25 inclusive; and the third between 12 and 23 inclusive. This is clearly not what you wanted, since some of the values that you’re trying to match fall out of those ranges.
I realize that I forgot the order of operations, so this information is incorrect. I believe that your use of Math.random() is correct.

QUOTE(Squirrel @ 2 Jun, 2007 - 03:30 PM) *
I understand actually what they are supposed to do, but I still cant get this line of code to work in IE. The image tag in question is named "randomprogram" so it should adjust it accordingly but it wont. Any ideas?
  • randomprogram is not a valid element name in HTML 4.01. If you want to add an image, you use the img or object elements.
  • Once you create the element using createElement(), you need to add it to the document by using something like appendChild() or insertBefore().
  • If you’re altering the src attribute of an existing image, you wouldn’t use createElement() at all. Instead, you would target the element through the use of getElementById(), getElementsByTagName(), or some other method. If you’re trying to target it by reference to a name attribute, you shouldn’t be; those are obsolete (in favor of IDs) except on form control elements.
An example revision of your last code snippet as it applies to an existing image:

CODE
<img id="content" alt="…" width="…" height="…" src="…">
CODE
var image = document.getElementById("randomprogram");
var rndprog = Math.floor((2 * Math.random()) + 1);
switch(rndprog) {
    case 1:
        image.setAttribute("src", "images/thumb_insultgenerator.jpg");
        break;    
    case 2:    
        image.setAttribute("src", "images/thumb_income.jpg");
        break;
}

If the image is meaningless without JavaScript, then I would create and insert it entirely using JavaScript so that it’s not seen when the user has JavaScript disabled. For example, if the alt, width, height, or src attributes are initially missing from the source (or left blank) because they need to be filled in via script, then this would apply. Example:

CODE
<div id="gallery"></div>
CODE
var image = document.createElement("img");
var rndprog = Math.floor((2 * Math.random()) + 1);
switch(rndprog) {
    case 1:
        image.setAttribute("alt", "<description>");
        image.setAttribute("width", "<width>");
        image.setAttribute("height", "<height>");
        image.setAttribute("src", "images/thumb_insultgenerator.jpg");
        break;    
    case 2:    
        image.setAttribute("alt", "<description>");
        image.setAttribute("width", "<width>");
        image.setAttribute("height", "<height>");
        image.setAttribute("src", "images/thumb_income.jpg");
        break;
}
document.getElementById("gallery").appendChild(image);

Note that the width and height attributes are recommended, but optional. The alt attribute is required in HTML 4.01.

This post has been edited by Arbitrator: 2 Jun, 2007 - 10:07 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month