random number generation

Discussion in 'Javascript' started by Fan924, Sep 10, 2008.

  1. Fan924

    Fan924 Guest

    How do I add random number generation to this background image slide
    show? Everything I try kills if. TIA
    _____________________________________
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;
    charset=windows-1252">
    <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <title>New Page 2</title>
    </head>
    <body>
    <SCRIPT LANGUAGE="JavaScript1.2">
    var pic=new Array()
    pic[0]="pic/./000.bmp"
    pic[1]="pic/./001.jpg"
    pic[2]="pic/./002.jpg"
    pic[3]="pic/./003.jpg"
    pic[4]="pic/./004.jpg"
    pic[5]="pic/./005.jpg"
    pic[6]="pic/./006.jpg"
    pic[7]="pic/./007.jpg"
    pic[8]="pic/./008.jpg"
    pic[9]="pic/./009.jpg"
    var img=new Array()
    for (i=0;i<pic.length;i++){
    img=new Image()
    img.src=pic
    }
    var enf=-1
    function Slide(){
    if (enf<pic.length-1)
    enf++
    else
    enf=0
    document.body.background=img[enf].src
    }
    if (document.all||document.getElementById)
    window.onload=new Function('setInterval("Slide()",1000)')
    </SCRIPT></BODY></HTML>
    Fan924, Sep 10, 2008
    #1
    1. Advertising

  2. Fan924 wrote:
    > How do I add random number generation to this background image slide
    > show? Everything I try kills if. TIA
    > [...]
    > var enf=-1
    > function Slide(){
    > if (enf<pic.length-1)
    > enf++
    > else
    > enf=0
    > document.body.background=img[enf].src
    > }


    You should try to learn how to write legible code first. After that it
    should become much easier for you to see the issue.

    However, as you are using FrontPage 4.0 and all other kinds of obsolete,
    error-prone, inefficient nonsense, and all of this could already be
    copy-and-pray in the first place, you may already be beyond help.

    <http://jibbering.com/faq/>


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
    Thomas 'PointedEars' Lahn, Sep 10, 2008
    #2
    1. Advertising

  3. Fan924 meinte:
    > How do I add random number generation to this background image slide
    > show? Everything I try kills if. TIA


    would be something like

    i = parseInt(Math.random() * pic.length, 10);

    Perhaps you should show what you'e tried so far. However, given the
    Frontpage crap you've posted, I can't think of anything meaningful.

    ["JS" atrocities snipped]

    Gregor



    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Sep 10, 2008
    #3
  4. Fan924

    RobG Guest

    Fan924 wrote:
    > How do I add random number generation to this background image slide
    > show? Everything I try kills if. TIA
    > _____________________________________
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html;
    > charset=windows-1252">
    > <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    > <meta name="ProgId" content="FrontPage.Editor.Document">
    > <title>New Page 2</title>
    > </head>
    > <body>
    > <SCRIPT LANGUAGE="JavaScript1.2">


    The language attribute was deprecated many years ago, type is reqired, use:

    <script type="text/javascript">


    Indented code is easier to read for most of us.


    > var pic=new Array()
    > pic[0]="pic/./000.bmp"
    > pic[1]="pic/./001.jpg"
    > pic[2]="pic/./002.jpg"
    > pic[3]="pic/./003.jpg"
    > pic[4]="pic/./004.jpg"
    > pic[5]="pic/./005.jpg"
    > pic[6]="pic/./006.jpg"
    > pic[7]="pic/./007.jpg"
    > pic[8]="pic/./008.jpg"
    > pic[9]="pic/./009.jpg"


    That can be written more concisely as an Array literal:

    var pic = ["pic/./000.bmp", "pic/./001.jpg",
    "pic/./002.jpg", "pic/./003.jpg",
    ...
    ];


    > var img=new Array()


    var img = [];


    > for (i=0;i<pic.length;i++){


    Don't forget to declare variables. It doesn't really matter here but is
    a good habbit:

    for (var i=0; i<pic.length; i++){


    > img=new Image()
    > img.src=pic
    > }
    > var enf=-1


    I understand why you've used -1 here, but consider using 0 and setting
    your background image to pic/./000.bmp otherwise your visitors will have
    to wait for the entire page and all images to load, then another second
    before any background image is shown. So use:

    <body style="background-image: url('pic/./000.bmp');">


    > function Slide(){


    By convention, function names that start with capital letters are
    reserved for constructors.

    > if (enf<pic.length-1)
    > enf++
    > else
    > enf=0


    That can be written as:

    var enf = 0;
    function Slide() {
    enf = ++enf % pic.length;


    > document.body.background=img[enf].src


    document.body.style.backgroundImage = 'url("'+img[enf].src+'")';


    > }
    > if (document.all||document.getElementById)


    The intention of feature detection is that you test for the feature that
    you want to use. It is a bad strategy to test for some other feature to
    infer support for the one you actually want to use.


    > window.onload=new Function('setInterval("Slide()",1000)')


    The Function constructor is unnecessary here and equivalent to the use
    of eval, use:

    window.onload = function(){
    window.setInterval(slide, 1000);
    }


    --
    Rob
    RobG, Sep 10, 2008
    #4
  5. In comp.lang.javascript message <sjQxk.30$>,
    Wed, 10 Sep 2008 15:44:47, Gregor Kofler <>
    posted:
    >Fan924 meinte:
    >> How do I add random number generation to this background image slide
    >> show? Everything I try kills if. TIA

    >
    >would be something like
    >
    >i = parseInt(Math.random() * pic.length, 10);


    Please read the standard about what parseInt is *for*, and the FAQ for
    Random(). Granted the best answer is not unlike that, but why post
    *that*.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr J R Stockton, Sep 10, 2008
    #5
  6. Dr J R Stockton meinte:
    > In comp.lang.javascript message <sjQxk.30$>,
    > Wed, 10 Sep 2008 15:44:47, Gregor Kofler <>
    > posted:


    >> i = parseInt(Math.random() * pic.length, 10);

    >
    > Please read the standard about what parseInt is *for*, and the FAQ for
    > Random(). Granted the best answer is not unlike that, but why post
    > *that*.


    Right doc - that's bullshit. Should have been Math.round() or rather
    Math.floor() - in fact, that's what I use in my own library. Sorry.

    Gregor


    --
    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
    http://web.gregorkofler.com ::: meine JS-Spielwiese
    http://www.image2d.com ::: Bildagentur für den alpinen Raum
    Gregor Kofler, Sep 10, 2008
    #6
  7. On Wed, 10 Sep 2008 at 23:54:15, in comp.lang.javascript, RobG wrote:
    >Fan924 wrote:
    >> How do I add random number generation to this background image slide
    >> show? Everything I try kills if. TIA
    >> _____________________________________
    >> <html>
    >> <head>
    >> <meta http-equiv="Content-Type" content="text/html;
    >> charset=windows-1252">
    >> <meta name="GENERATOR" content="Microsoft FrontPage 4.0">
    >> <meta name="ProgId" content="FrontPage.Editor.Document">
    >> <title>New Page 2</title>
    >> </head>
    >> <body>
    >> <SCRIPT LANGUAGE="JavaScript1.2">

    >
    >The language attribute was deprecated many years ago, type is reqired, use:
    >
    > <script type="text/javascript">


    Except that the draft for HTML 5 says the type attribute can be omitted
    if the type is text/javascript, and the language attribute is no longer
    deprecated (though its meaning has changed a little). :-(


    >Indented code is easier to read for most of us.

    <snip>

    Hear, hear!

    John
    --
    John Harris
    John G Harris, Sep 12, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,072
    vipinlal
    Mar 26, 2010
  2. ANM
    Replies:
    2
    Views:
    1,358
    Thomas Schodt
    Mar 7, 2004
  3. mescaline
    Replies:
    4
    Views:
    1,332
    Cy Edmunds
    Sep 10, 2003
  4. globalrev
    Replies:
    4
    Views:
    753
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,154
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page