venu said:
I have a scenario where in my HTML page which gets rendered at
the server in CGI contains a table. For a particular column in that
table and for each row, I will get an epoch value from the server
which I've to convert to Human readable format. I tried calling that
Javascript function as shown in the below code but it didn't work.
I am new to Javascript.
But you are not new to programming, are you? So the first thing you
should have considered is debugging, and providing a *useful* error
description in your posting, including the error message provided by
the runtime environment if debugging did not help.
<
http://jibbering.com/faq/#posting> pp.
However:
So, can someone help me in this regard.
CGI Script
CGI (Common Gateway Interface) is but an interface specification; it would
have been helpful to tell the programming language that you are using with
it (e.g., Bourne-Shell compatible script, Perl, PHP, Python).
I doubt this is a meaningful statement in any programming language.
Probably you have omitted too much here.
{
<tr ...>
...
html .= "<td id = "epoch" onload='convert_time(".$epoch.");'></td>";
This generates invalid HTML, see below. Besides, for this to work client-
side (or be Valid to begin with; ignoring the other problems, see below) it
matters what `$epoch' evaluates to, i.e. what the *client* receives.
...
}
Javascript:
convert_time(epoch_time)
Assuming that you have not omitted too much here:
The first thing one should do before first writing code in a programming
language is to RTFM of that language, to become familiar with its syntax
and semantics. This is J(ava)Script/ECMAScript, not e.g. Bourne-compatible
Shell-script. By contrast, you have not declared a function here, you have
tried to call one. As you apparently wanted to declare it instead, it was
probably not declared or defined before, so that unintentional call would
need to fail as `convert_time' could either not be resolved, or did not
resolve to a reference to a callable object.
So you probably want to precede this with the `function' keyword to make it
a function declaration.
{
.... // Processing
document.getElementById('epoch').innerHTML= tme;
}
Because the entire snippet could not be produced by the
/FunctionDeclaration/ production, and automatic semicolon insertion must
place the semicolon after the unintentional function call, the braces are
parsed as a Block statement. As a result, if the preceding statement did
not cause program abortion already, the statements in the Block statement
are immediately executed. It is possible that this would cause a(nother)
runtime error, for example if the elements being referred to had not yet
been parsed.
I tried to put the code as simple as possible. The <td> repeats in a
loop for each row and this function should be invoked for each row's
td. Please let me know if you need any more clarifications.
You really should learn the basics, in this case HTML, first. IDs must be
unique throughout a document (i.e. two elements MUST NOT have the same ID),
and TD elements certainly have no `onload' attribute. You can and should
search for those and further such mistakes by validating your markup:
<
http://validator.w3.org/>
Perhaps you wanted to use the `onload' attribute of the BODY element to
call a function that used *different* `id' attribute values or same `class'
attribute values to fill the respective elements. But then the question
remained: Why would you not simply generate the element content with the
CGI script?
All in all, your current concept appears to lack severely in being thought
through. As always, you should try to explain what you want to achieve
instead of what problems you had to achieve it the way you thought, because
especially at your current experience level it is very likely, and almost
certain here, that you chose an unwise or completely wrong approach.
PointedEars