Javascript Syntax

Discussion in 'HTML' started by Cogito, Jun 14, 2004.

  1. Cogito

    Cogito Guest

    I'm using code from another web site to do some javascript code and
    I'm completely not familiar with it… so it is trial and error…

    Can someone please show me the correct javascript syntax for this
    function?

    k=i*16^(j-1)

    k equals i times 16 to the power of (j-1)

    Is there an online javascript reference guide?
     
    Cogito, Jun 14, 2004
    #1
    1. Advertising

  2. *Cogito* wrote:
    > I'm using code from another web site to do some javascript code and
    > I'm completely not familiar with it. so it is trial and error.
    >
    > Can someone please show me the correct javascript syntax for this
    > function?
    >
    > k=i*16^(j-1)
    >
    > k equals i times 16 to the power of (j-1)


    var k = i * Math.pow(16, j - 1)

    > Is there an online javascript reference guide?


    http://www.google.com/search?q=online javascript reference
    --
    Andrew Urquhart
    - FAQ: www.html-faq.com
    - Archive: www.google.com/groups?q=alt.html
    - My reply address is invalid, use: www.andrewu.co.uk/contact/
     
    Andrew Urquhart, Jun 14, 2004
    #2
    1. Advertising

  3. Cogito

    Cogito Guest

    On Mon, 14 Jun 2004 11:12:46 +0100, "Andrew Urquhart"
    <> wrote:

    >*Cogito* wrote:
    >> I'm using code from another web site to do some javascript code and
    >> I'm completely not familiar with it. so it is trial and error.
    >>
    >> Can someone please show me the correct javascript syntax for this
    >> function?
    >>
    >> k=i*16^(j-1)
    >>
    >> k equals i times 16 to the power of (j-1)

    >
    >var k = i * Math.pow(16, j - 1)
    >
    >> Is there an online javascript reference guide?

    >
    >http://www.google.com/search?q=online javascript reference


    Thank you for the answer.

    Now that I got the first part to work I have a follow on question: in
    the variable k I have a value between 0 and 15. I want to display it
    as a hex character between 0 and F.
    Is there a quick way of doing it?

    Steve
     
    Cogito, Jun 14, 2004
    #3
  4. *Cogito* wrote:
    > Now that I got the first part to work I have a follow on question: in
    > the variable k I have a value between 0 and 15. I want to display it
    > as a hex character between 0 and F.
    > Is there a quick way of doing it?


    var strData = k.toString(16);
    --
    Andrew Urquhart
    - FAQ: www.html-faq.com
    - Archive: www.google.com/groups?q=alt.html
    - My reply address is invalid, use: www.andrewu.co.uk/contact/
     
    Andrew Urquhart, Jun 14, 2004
    #4
  5. Cogito

    Cogito Guest

    On Mon, 14 Jun 2004 15:26:16 +0100, "Andrew Urquhart"
    <> wrote:

    >*Cogito* wrote:
    >> Now that I got the first part to work I have a follow on question: in
    >> the variable k I have a value between 0 and 15. I want to display it
    >> as a hex character between 0 and F.
    >> Is there a quick way of doing it?

    >
    >var strData = k.toString(16);


    This is all magic.
    Is it by any chance possible to make it show the' a' to 'f' hex values
    in capitals?

    Steve
     
    Cogito, Jun 15, 2004
    #5
  6. Cogito

    rf Guest

    "Cogito" <> wrote in message
    news:...
    > On Mon, 14 Jun 2004 15:26:16 +0100, "Andrew Urquhart"
    > <> wrote:
    >
    > >*Cogito* wrote:
    > >> Now that I got the first part to work I have a follow on question: in
    > >> the variable k I have a value between 0 and 15. I want to display it
    > >> as a hex character between 0 and F.
    > >> Is there a quick way of doing it?

    > >
    > >var strData = k.toString(16);

    >
    > This is all magic.


    No, it's not. It's all detailed in the javascript references :)

    > Is it by any chance possible to make it show the' a' to 'f' hex values
    > in capitals?


    var u = strData.toUpperCase();

    Cheers
    Richard.
     
    rf, Jun 15, 2004
    #6
  7. Cogito wrote:
    >Andrew Urquhart wrote:

    <snip>
    >>var strData = k.toString(16);

    >
    > This is all magic.
    > Is it by any chance possible to make it show the' a' to 'f'
    > hex values in capitals?


    var strData = k.toString(16).toUpperCase();

    Richard.
     
    Richard Cornford, Jun 15, 2004
    #7
  8. Cogito

    Cogito Guest

    On Mon, 14 Jun 2004 09:59:19 GMT, Cogito <> wrote:

    Using as an example some code that I saw in a web site I managed to
    create code that builds a hex to decimal table (which is incidental to
    my questions).

    I find it amazing how in such a small javascript program I can
    generate such a sophisticated table including the generation of the
    HTML code that builds the table. I'm sure that to the experts amongst
    you this is old stuff.

    After coding it I still have three questions:

    1. In the sample code the whole thing works when invoked in the <BODY>
    line:

    <BODY leftMargin=0 onload=hextable() topMargin=0>

    When I remove the 0 onload=hextable() from this line and place it as

    hextable();

    it does not work.

    Any suggestion how I can activate it from within the html part of the
    code?

    2. Despite the fact that the code seems to generate the entire table
    it still requires a table definition at the bottom part of the code
    (it does not work when I remove it). What is the explanation for this?

    3. What is the meaning of the line:
    sign.innerHTML=area

    My sample code is here:
    http://users.bigpond.net.au/blackbox/hex.html
     
    Cogito, Jun 16, 2004
    #8
  9. Cogito

    rf Guest

    "Cogito" <> wrote in message
    news:...
    > On Mon, 14 Jun 2004 09:59:19 GMT, Cogito <> wrote:
    >
    > Using as an example some code that I saw in a web site I managed to
    > create code that builds a hex to decimal table (which is incidental to
    > my questions).


    > I find it amazing how in such a small javascript program I can
    > generate such a sophisticated table including the generation of the
    > HTML code that builds the table. I'm sure that to the experts amongst
    > you this is old stuff.
    >
    > After coding it I still have three questions:
    >
    > 1. In the sample code the whole thing works when invoked in the <BODY>
    > line:
    >
    > <BODY leftMargin=0 onload=hextable() topMargin=0>
    >
    > When I remove the 0 onload=hextable() from this line and place it as
    >
    > hextable();


    > it does not work.


    Do you mean like
    <body hextable() ...>

    If so then this is invalid. hextable() is being treated as an (invalid)
    attribute of the <body> tag.

    If you mean like
    <body>
    hextable()
    ....

    then hextable() is merely content.

    Perhaps you mean

    <body>
    ....
    <script type="text/javascript">
    hextable();
    </script>

    In this case, however, the javascript must appear after (in your HTML) the
    element whose id is sign.

    > Any suggestion how I can activate it from within the html part of the
    > code?


    Er, see above.

    > 2. Despite the fact that the code seems to generate the entire table
    > it still requires a table definition at the bottom part of the code
    > (it does not work when I remove it). What is the explanation for this?


    No, it does not. All it needs is an element whose id is "sign". You have
    provided this in your td element. What you really have is a nested table.
    Turn on table borders (for both tables) and you will see what is hapenning.

    Methinks what you really want is

    <div id="sign"></div>
    <script type="text/javascript">
    hextable();
    </script>

    where the div is a catch all block level element avaible for use when no
    other element fits the bill.

    > 3. What is the meaning of the line:
    > sign.innerHTML=area


    Area is a var where you have dumped a lot of text into. This text is,
    incidentally, sort of valid HTML.

    There is an element on the page whose id is sign.

    sign.innerHTML=area plonks the content of your area var into the element, as
    HTML, just as if you had hand coded it within that element when you wrote
    the page.

    Look up the specs on innerText. This plonks *content* into an element.
    innerHTML plonks html code into the element. Yes, the browser reparses the
    document object model and re-lays out the page. You may also like to explore
    sign.innerText=area;

    Of course innerHTML is a microsoft invention and as such will not work on
    non-IE browsers.

    Cheers
    Richard.
     
    rf, Jun 16, 2004
    #9
  10. Cogito wrote:

    > 2. Despite the fact that the code seems to generate the entire table
    > it still requires a table definition at the bottom part of the code
    > (it does not work when I remove it). What is the explanation for this?


    Scroll down to where I've put a double asterisk. There's your explanation.

    <SCRIPT language=javascript>
    function hextable()
    {
    area="<table border=0 cellspacing=1 cellpadding=1 width='100%'><tr>"

    for (k = 1 ; k <= 8 ; k++ )
    {
    area+="<td class='headings' colspan=2>"+k
    }
    area+="</tr><tr>"

    for (k = 1 ; k <= 8 ; k++ )
    {
    area+="<td class='headings'>"+"Dec"+"</td><td class='headings'>"+"Hex"+"</td>"
    }
    area+="</tr><tr>"

    for (k = 0 ; k <= 15 ; k++ )
    {
    j=1;
    for ( i = 1 ; j <= 8 ; i=i*16 )
    {
    area+="<td class='dec'>"+i*k+"</td><td class='hex'>"+k.toString(16).toUpperCase()+"</td>"
    j++
    }
    area+="</tr><tr>" // ** opens an extra table row at the end.
    }

    area+="</table>"
    sign.innerHTML=area
    }
    </SCRIPT>

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
     
    Toby A Inkster, Jun 16, 2004
    #10
  11. Cogito

    rf Guest

    "Toby A Inkster" <> wrote in message
    news:p...
    > Cogito wrote:
    >
    > > 2. Despite the fact that the code seems to generate the entire table
    > > it still requires a table definition at the bottom part of the code
    > > (it does not work when I remove it). What is the explanation for this?

    >
    > Scroll down to where I've put a double asterisk. There's your explanation.


    Which IE will happily error correct away.

    Cheers
    Richard.
     
    rf, Jun 16, 2004
    #11
  12. *rf* wrote:
    > Of course innerHTML is a microsoft invention and as such will not
    > work on non-IE browsers.


    For the sake of correctness I feel I should point out:
    http://www.developer-x.com/content/innerhtml/

    - where compatible browsers are listed as:

    "
    Microsoft Internet Explorer 4+
    Microsoft Internet Explorer 5 Mac
    Opera 7+ (November 2002)
    Mozilla M17+
    Netscape 6+
    Konqueror 2.2+
    IceStorm 5.
    iCab 2.x+
    MS Pocket IE 3.x+
    "

    Safari also supports innerHTML, as seen at
    http://www.quirksmode.org/dom/innerhtml.html
    --
    Andrew Urquhart
    - FAQ: www.html-faq.com
    - Archive: www.google.com/groups?q=alt.html (Google Groups)
    - My reply address is invalid, use: www.andrewu.co.uk/contact/
     
    Andrew Urquhart, Jun 16, 2004
    #12
  13. Cogito

    rf Guest

    "Andrew Urquhart" <> wrote in
    message news:L2Wzc.69$Y75.34@newsfe6-win...
    > *rf* wrote:
    > > Of course innerHTML is a microsoft invention and as such will not
    > > work on non-IE browsers.

    >
    > For the sake of correctness I feel I should point out:
    > http://www.developer-x.com/content/innerhtml/
    >
    > - where compatible browsers are listed as:


    [ list of browsers ]

    I stand corrected. I was going on microsofts documetation where it says
    "there is no public standard that applies to this property". Also the fact
    that AFAIK it is not in the spec.

    It's good to see that the non-IE browsers do support some of the MS stuff
    that is actually of use :)

    Cheers
    Richard.
     
    rf, Jun 16, 2004
    #13
  14. Cogito

    Cogito Guest

    Thanks for your replies. You are of great help to me.

    In Question 1, I meant:

    <body>
    ....
    <script type="text/javascript">
    function hextable()
    {

    }
    </script>
    other HTML code…

    and then call the functiom…
    hextable();

    Is this possible? Why is it invoked on <BODY> line:

    <BODY leftMargin=0 onload=hextable() topMargin=0>

    and not:

    <BODY leftMargin=0 topMargin=0>
    hextable();

    When I try it it does not work.



    In Question 2:

    Your suggestion to use <DIV… works and is much simpler and less code
    than the <TABLE>.. code. What exactly does the <DIV> statement do?

    I'm glad to read from the follow on posts that this type of coding is
    legitimate. I think it is so cool… How would you be able to do such a
    table otherwise? That is, without hard coding the entire table and
    having the calculated values hard coded in the HTML?

    Is innerHTML (now I can use the correct jargon like an expert) common?
    Do you have links to sites that use it?
     
    Cogito, Jun 16, 2004
    #14
  15. rf wrote:

    > "Toby A Inkster" <> wrote in message
    > news:p...
    >> Cogito wrote:
    >>
    >> > 2. Despite the fact that the code seems to generate the entire table
    >> > it still requires a table definition at the bottom part of the code
    >> > (it does not work when I remove it). What is the explanation for this?

    >>
    >> Scroll down to where I've put a double asterisk. There's your explanation.

    >
    > Which IE will happily error correct away.


    For some reason I read the OP as saying that it required an extra
    "</table>" tag. I should have read more carefully.

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me - http://www.goddamn.co.uk/tobyink/?page=132
     
    Toby A Inkster, Jun 17, 2004
    #15
  16. Cogito

    rf Guest

    "Toby A Inkster" <> wrote in message
    news:p...
    > rf wrote:
    >
    > > "Toby A Inkster" <> wrote in message
    > > news:p...
    > >> Cogito wrote:
    > >>
    > >> > 2. Despite the fact that the code seems to generate the entire table
    > >> > it still requires a table definition at the bottom part of the code
    > >> > (it does not work when I remove it). What is the explanation for

    this?
    > >>
    > >> Scroll down to where I've put a double asterisk. There's your

    explanation.
    > >
    > > Which IE will happily error correct away.

    >
    > For some reason I read the OP as saying that it required an extra
    > "</table>" tag. I should have read more carefully.


    Yeah, it took me a couple of goes to figure out what the OP meant as well,
    especially the bit about it not working when he took it away :)

    --
    Cheers
    Richard.
     
    rf, Jun 17, 2004
    #16
    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. gabriele renzi
    Replies:
    2
    Views:
    212
    gabriele renzi
    Dec 31, 2005
  2. Ken Bloom
    Replies:
    3
    Views:
    220
  3. Good Night Moon
    Replies:
    9
    Views:
    285
    Rick DeNatale
    Jul 25, 2007
  4. Jacob Grover
    Replies:
    5
    Views:
    321
    Jacob Grover
    Jul 18, 2008
  5. Mark Richards
    Replies:
    3
    Views:
    320
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page