fusillo said:
I've tried to use the call method of Function object assigning this
explicitly, but i've some problem to detach the listener, actually i
don't find a solution in my new test code. Here's:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
You should discard XHTML 1.0 Transitional in favor of HTML 4.01 Transitional
(or better: Strict) immediately. You do not need XHTML here at all, and it
only gets you into more trouble than you realize(d).
<head>
<style type="text/css">
This is not supposed to work in XHTML. It is an XML application, therefore
XML stylesheet rules apply. In XML, the stylesheet to be used to render a
document must be defined via an <?xml-stylesheet ... ?> processing
instruction (PI) before the DOCTYPE declaration. This can be facilitated
with
<?xml-stylesheet href="#foo" type="text/css"?>
<!DOCTYPE ...>
<html ...>
<head ...>
...
<style type="text/css" id="foo">
...
</style>
...
</head>
...
</html>
But it is recommended that you refer to an external stylesheet resource
instead (because there is also the PCDATA issue, see below):
<URL:
http://www.w3.org/TR/xhtml1/#C_13>
It "works" anyway because you serve XHTML as text/html, instead of the
recommended media type application/xhtml+xml. This will have UAs with
tag soup parsers to parse XHTML as if it were wrongfully error-corrected
HTML. Search the archives.
.cliccabile {background-color: #FFCCCC; cursor
ointer;}
.cliccato {background-color: #FF6666;}
.festivo {color: #009966;}
#tabella{background-color:#FFFFFF;}
Use Web-safe triplets (#fcc, #f66, #096, #fff), and take heed of
</style>
</head>
<body>
<div id="tabella"></div>
If this was parsed by an XML parser, you could write it as
<div id="tabella"/>
(If this does not work, it is a proof that an XML parser is _not_ used.)
<script type="text/javascript">
Correct.
<!--
This long-ago-obsolete ever-nonsense will get you into trouble when
a) an SGML parser is used to parse this as HTML, or b) an XML parser
is rightfully used to parse what you declared as XHTML.
a) The content model of the HTML `script' element is (and has been) CDATA,
so it is not parsed by the markup parser. However, that means the script
engine is passed the comment delimiter, which should be regarded as a
syntax error. You are relying on a proprietary, hardly documented
ECMAScript extension that allows a script engine to discard this
character sequence (and all characters that follow it up to the line
terminator) if it occurs in a line; this is error-prone.
And RFC2854 (June 2000 CE) eventually obsoleted HTML versions prior to
HTML 3.2 (January 1997 CE), so every HTML UA that can be considered
working MUST know about the `script' element (even if it does not support
client-side scripting). There is no reason for trying to hide anything
here except of the misguided attempt of keeping completely b0rken
software alive. (Do you really want that?)
b) Since the content model of the _XHTML_ `script' element is PCDATA, it is
parsed by the markup parser (in contrast to CDATA content). And an XML
parser regards this as a comment that it is allowed to remove. You
and have none said:
function myAddListener(obj,strtype,listener,usecapture){
if (obj.addEventListener)
obj.addEventListener(strtype,listener,usecapture);
else if (obj.attachEvent){
obj.attachEvent('on'+strtype,function myCall(evt){ window.alert('I
view myCall function'); listener.call(obj,evt); });
}
//else it throws an Error exception
But it does not throw an exception then.
}
function myRemoveListener(obj,strtype,listener,usecapture){
if (obj.removeEventListener)
obj.removeEventListener(strtype,listener,usecapture);
else if (obj.detachEvent){
myCall(null); ^^^^^^^^^^^^
obj.detachEvent('on'+strtype,myCall);//I view myCall function but i ^^^^^^
don't detach the event
`myCall' is undefined, and cannot be called.
}
//else it throws an Error Exception
Again, it does not.
}
function myListener(evt){
window.alert('click');
var oldclickedcell=document.getElementById('DOMtable').clickedcell;
You SHOULD NOT try to augment host objects in order to store user-defined
data; use native objects instead.
if (oldclickedcell){
oldclickedcell.className=oldclickedcell.className.replace('cliccato','cliccabile');
myAddListener(oldclickedcell,'click',myListener,false);
}
var
newclickedcell=(this.className&&this.className.indexOf('cliccabile')>-1?this:false);
(<URL:
http://www.w3.org/TR/xhtml1/#h-4.8>)
An XML parser must regard the character data of the `script' element to end
here because of the `>' which is a specified as a markup character. (Point
b) above and this, is the reason why you get a parse error when you do
not "comment out" the `script' element's content, and you do not get the
error if you "comment it out".)
There are different _reasonable_ ways to avoid that; in recommended order:
1. Declare HTML instead of XHTML, and replace XML syntax with SGML syntax.
Watch that you escape ETAGO (`</') delimiters in the (then) CDATA-typed
script code, preferably via `<\/'.
or
Move the script code to an external resource and include that resource
with <script type="text/javascript" src="external_resource.js"/> (XHTML),
or <script type="text/javascript" src="external_resource.js"></script>
(XHTML, and HTML 4.01), respectively.
2. Declare the content of the element to be a CDATA section that is not to
be parsed:
<script type="text/javascript">
<![CDATA[
...
]]>
</script>
3. Escape all markup characters in the script code:
<script type="text/javascript">
...
if (1 <= 2 && (4 << 1) >= 3)
{
window.alert(42);
}
</script>
(2. and 3. are XHTML-only solutions.)
Source code should be posted so that it does not exceed 80 columns per line.
function handlerGo(table_id){
document.getElementById(table_id).clickedcell=false;
See above. Don't do that.
[...]
strtabella="<table id=\"DOMtable\"><tr><td id=\"2006-5-4\"
class=\"cliccabile festivo\">4</td><td id=\"2006-5-5\"
class=\"cliccabile\">5</td></tr></table>";
document.getElementById('tabella').innerHTML=strtabella;
`innerHTML' is a proprietary property, while document.getElementById()
is part of a Web standard. Do not mix both, at least not without
feature-testing each part of the reference.
newclickedcell=document.getElementById('DOMtable').rows.item(0).cells.item(0);
Do not use reference worms, see above. And you do not need `.items(...)',
you can use [...] instead; the same goes for `.namedItems("...")' which can
be replaced by `["..."]'.
[...]
// -->
</script>
</body>
</html>
I'm conscious about importance of getElementById feature detection, but
i don't care it in this test case.
Your problem. You should always care.
Every advice to better my programmation style is really
well-appreciated.
[x] done
You are welcome.
PointedEars