Velocity Reviews > HTML > Javascript Syntax

# Javascript Syntax

Cogito
Guest
Posts: n/a

 06-14-2004
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?

Andrew Urquhart
Guest
Posts: n/a

 06-14-2004
*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?

--
Andrew Urquhart
- FAQ: www.html-faq.com

Cogito
Guest
Posts: n/a

 06-14-2004
On Mon, 14 Jun 2004 11:12:46 +0100, "Andrew Urquhart"
<(E-Mail Removed)> 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?

>

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

Andrew Urquhart
Guest
Posts: n/a

 06-14-2004
*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

Cogito
Guest
Posts: n/a

 06-15-2004
On Mon, 14 Jun 2004 15:26:16 +0100, "Andrew Urquhart"
<(E-Mail Removed)> 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

rf
Guest
Posts: n/a

 06-15-2004

"Cogito" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mon, 14 Jun 2004 15:26:16 +0100, "Andrew Urquhart"
> <(E-Mail Removed)> 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.

Richard Cornford
Guest
Posts: n/a

 06-15-2004
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.

Cogito
Guest
Posts: n/a

 06-16-2004
On Mon, 14 Jun 2004 09:59:19 GMT, Cogito <(E-Mail Removed)> 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:

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

rf
Guest
Posts: n/a

 06-16-2004

"Cogito" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mon, 14 Jun 2004 09:59:19 GMT, Cogito <(E-Mail Removed)> 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:
>
>
> 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.

Toby A Inkster
Guest
Posts: n/a

 06-16-2004
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()
{

for (k = 1 ; k <= 8 ; k++ )
{
}
area+="</tr><tr>"

for (k = 1 ; k <= 8 ; k++ )
{
}
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