Javascript Syntax

C

Cogito

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?
 
A

Andrew Urquhart

*Cogito* said:
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);
 
R

Richard Cornford

Cogito said:
Andrew Urquhart wrote:

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

Cogito

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
 
R

rf

Cogito said:
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.
 
T

Toby A Inkster

Cogito said:
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>
 
A

Andrew Urquhart

*rf* said:
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
 
R

rf

Andrew Urquhart said:
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.
 
C

Cogito

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?
 
T

Toby A Inkster

rf said:
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.
 
R

rf

Toby A Inkster said:
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 :)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top