Generated cells in HTML table not showing

Discussion in 'Javascript' started by SandyTipper, Aug 12, 2009.

  1. SandyTipper

    SandyTipper Guest

    I thought this was a trivial bit of code, but it doesn't seem to work.
    Anybody wlling to help?
    I create a simple one-row html table, and insert a javascript piece
    between the <tr> and </tr>, which should use document.write to
    create three columns to display random ads for my site's sponsors.

    <P align=center>
    <TABLE style="BORDER-BOTTOM: #999999 1px solid; BORDER-LEFT: #999999 1px
    solid; MARGIN: 1px; BORDER-COLLAPSE: collapse; BORDER-TOP: #999999 1px
    solid; BORDER-RIGHT: #999999 1px solid" class=contStyleExcSimpleTable
    cellSpacing=0 cellPadding=3 width="97%" bgColor=#ffffff align=left>
    <TBODY>
    <TR><script type="text/javascript">
    var myAds=[]; //will contain the ads

    // Edit here: Put the cell contents in myAds elements

    myAds[0]='<H3>Hair Spa</H3><blockquote>ad content</blockquote>';
    myAds[1]='<H3>This Space for Rent</H3>';
    myAds[2]='<H3>Ads: $35/year</H3>';
    myAds[3]='<H3>Your Ad Here</H3>';

    // End of editing
    var myCell=[]; // random indexes into myAds; 3 elements
    myCell[0]=Math.floor(Math.random()*myAds.length);
    do {
    myCell[1]=Math.floor(Math.random()*myAds.length);
    } while (myAds.length > 2 && myCell[1] != myCell[0]);
    do {
    myCell[2]=Math.floor(Math.random()*myAds.length);
    } while (myAds.length > 2 && myCell[2] != myCell[0] && myCell[2] !=
    myCell[1]);

    for (var i=0; i<3; i++){
    document.write('<TD style="width: 33%;VERTICAL-ALIGN: top;">';
    document.write(myAds[myCell]);
    document.write('</TD>');
    }
    </script></TR></TBODY></TABLE></P>
     
    SandyTipper, Aug 12, 2009
    #1
    1. Advertisements

  2. SandyTipper

    SandyTipper Guest


    Oh, yes, if you're wondering why I didn't do this server-side so it
    does not depend on clients permitting javascript, I can't. Not my server.
     
    SandyTipper, Aug 12, 2009
    #2
    1. Advertisements

  3. SandyTipper

    Luuk Guest

    SandyTipper schreef:
    so, you want to make money with your website...?

    and you cannot even count if you have every '(' properly closed with a
    ')'....


    and all that 'claiming' that is not your server.....
    What is the difference that its yours, or not?
     
    Luuk, Aug 12, 2009
    #3


  4. Just a few observations:
    1) "it doesn't seem to work" is as useful as a chocolate teapot. A
    description of what does happen and/or what error messages you get is
    far more helpful.
    2) explaining what you have done to try to find out what is wrong is
    also helpful.

    Running it in either FireFox (and looking at the error console) or IE8
    (with the developer tools switched on) reveals a syntax error sraight
    away.
     
    Captain Paralytic, Aug 12, 2009
    #4
  5. SandyTipper

    SandyTipper Guest

    No, it's a volunteer organization and we want to offset the cost of hosting.
    I don't care for your snippy tone, but I don't have admin rights,
    and the hosting service doesn't permit server-side scripting.
     
    SandyTipper, Aug 12, 2009
    #5
  6. SandyTipper

    Doug Miller Guest

    Then move the domain to a better hosting service. The one you have now is
    worthless.
     
    Doug Miller, Aug 12, 2009
    #6
  7. SandyTipper

    SandyTipper Guest

    ....

    Just a few observations:
    1) "it doesn't seem to work" is as useful as a chocolate teapot. A
    description of what does happen and/or what error messages you get is
    far more helpful.
    2) explaining what you have done to try to find out what is wrong is
    also helpful.

    Running it in either FireFox (and looking at the error console) or IE8
    (with the developer tools switched on) reveals a syntax error sraight
    away.
     
    SandyTipper, Aug 12, 2009
    #7
  8. You should say in what way it does not work.



    You should, before posting, remorselessly remove from the code all that
    is not necessary to demonstrate the not-working. Then test what's left;
    it may now work. You can, for example, remove all style; then we do not
    have to read it.

    Repeated code , such as
    Math.floor(Math.random()*myAds.length)
    should be put into a function Random, as in the FAQ, which you ought to
    have read.

    You know of the missing ).
    The first "2 &&" should be clearly "1 &&".

    Your logic is wrong; each != should be == , the last && should be ||,
    and you should consider whether that then needs more parentheses.



    Your algorithm is cumbersome. Your selection problem is equivalent to
    drawing three ads from a deck of four ads, which is like dealing, and
    reading the FAQ will lead you to what you need there.

    Deal(myAds.length) will generate a list of all ad indexes in random
    order :

    function Deal(N) { var J, K, Q = new Array(N)
    for (J=0 ; J<N ; J++)
    { K = Random(J+1) ; Q[J] = Q[K] ; Q[K] = J }
    return Q }

    and you just take what you need. For a vast number of ads it would be
    better to generate a partial list only; you now should be able to find
    code for that.
     
    Dr J R Stockton, Aug 13, 2009
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.