# Javascript Syntax

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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?

## Re: Javascript Syntax

*Cogito* wrote:

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

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

## Re: Javascript Syntax

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

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

## Re: Javascript Syntax

*Cogito* wrote:

var strData = k.toString(16);
--
Andrew Urquhart
- FAQ: www.html-faq.com

## Re: Javascript Syntax

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

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

Steve

## Re: Javascript Syntax

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

var u = strData.toUpperCase();

Cheers
Richard.

## Re: Javascript Syntax

Cogito wrote:

<snip>

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

Richard.

## Re: Javascript Syntax

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

## Re: Javascript Syntax

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.

Er, see above.

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.

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.

## Re: Javascript Syntax

*rf* wrote:

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

## Re: Javascript Syntax

[ 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.

## Re: Javascript Syntax

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:

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?

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?

## Re: Javascript Syntax

Cogito wrote:

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

## Re: Javascript Syntax

Which IE will happily error correct away.

Cheers
Richard.

## Re: Javascript Syntax

rf wrote:

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

## Re: Javascript Syntax

this?

explanation.

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.