setAttribute

S

Sri02

All,

I was trying to add an onclick event using setAttribute to a <td> from
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.
Here is the snippet for ur reference:
<...
var _table=document.createElement("table");
_tbody=document.createElement("tbody");

var _tr = document.createElement("tr");
var _td = document.createElement("td");
_td.innerHTML="6";
_td.id=""+x+"_"+y+"";
_td.setAttribute("onclick",function(){check(event);});
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
....
And here is function "Check's" definition
function check(e){
if (!e)
{
var e=window.event;
alert(e+" :None");
}
if (e.target)
{

targ=e.target;
alert("FF/Netscape: ");
}
else if (e.srcElement)
{
targ=e.srcElement;
alert("IE");
document.getElementById(e.srcElement.id).innerHTML="0";
}
alert(targ.tagName);
}

Any help in tracking this problem would be really useful.
 
T

Thomas 'PointedEars' Lahn

Sri02 said:
I was trying to add an onclick event using setAttribute to a <td> from
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.

setAttribute() has a number of buggy implementations, avoid it.
Here is the snippet for ur reference:
^^
This is not an Internet chat.
<...
var _table=document.createElement("table");
_tbody=document.createElement("tbody");

var _tr = document.createElement("tr");
var _td = document.createElement("td");
_td.innerHTML="6";

Using the proprietary innerHTML property is not necessary for setting
the content of an element to "6". Especially not when you are using
standards-compliant DOM methods already.

_td.appendChild(document.createTextNode("6"));
_td.id=""+x+"_"+y+"";

The leading and trailing concatenation is inefficient and superfluous.

_td.id = x + "_" + y;
_td.setAttribute("onclick",function(){check(event);});

As Jorge said.
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");

There is an extra said:

All of this should be secured by a runtime feature test before use, of course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}
And here is function "Check's" definition
function check(e){
if (!e)
{
var e=window.event;
alert(e+" :None");
}
if (e.target)

This should be at least

if (e)
{
var t = e.target || e.srcElement;
if (t)
{
// ...
}
}

No need to have a separate branch for each DOM.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
As Jorge said.

JFTR, the standards-compliant approach is:

_td.addEventListener("click", function(e) { check(e); }, false);

or

_td.addEventListener(
"click", {handleEvent: function(e) { check(e); }}, false);

But MSHTML does not support it and attachEvent() is not a viable alternative.


PointedEars
 
A

Aaron Gray

Thomas 'PointedEars' Lahn said:
Sri02 wrote:
All of this should be secured by a runtime feature test before use, of
course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}

Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned by
you ?

Sri02,
_td.setAttribute("onclick",function(){check(event);});

As for setting event handlers for both standards compliant and legacy
browser support see my "addEvent - The late entry :)" thread :-

http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/e7145c0d89caf0a1#

Aaron
 
T

Thomas 'PointedEars' Lahn

Aaron said:
Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned by
you ?

You may use any `isMethod' implementation you like as long as its license
allows it, including mine.
As for setting event handlers for both standards compliant and legacy
browser support see my "addEvent - The late entry :)" thread :-

http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/e7145c0d89caf0a1#

The approach you present there is old news and flawed, as is attachEvent().
I was going to reply to that in more detail later, although this has been
discussed to death here and elsewhere already.


PointedEars
 
A

Aaron Gray

Thomas 'PointedEars' Lahn said:
You may use any `isMethod' implementation you like as long as its license
allows it, including mine.

Okay, you should really mention things like that though.
The approach you present there is old news and flawed, as is
attachEvent().
I was going to reply to that in more detail later, although this has been
discussed to death here and elsewhere already.

Great Thomas, I shall look forward to that then.

Aaron
 
A

Aaron Gray

Thomas 'PointedEars' Lahn said:
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <[email protected]>

Thomas,

You seem to have made it impossible to find the original quoted source.

Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.

Thanks,

Aaron
 
T

Thomas 'PointedEars' Lahn

Aaron said:
Don't.


Okay, you should really mention things like that though.

Subscribers of this newsgroup are supposed to be intelligent beings who are
capable of typing "isMethod" into the text input of their favorite search
engine when they get a runtime error on calling isMethod().


PointedEars
 
T

Thomas 'PointedEars' Lahn

Aaron said:
Don't.

You seem to have made it impossible to find the original quoted source.

Yes, it only seems to be so.
Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.

Having yourself evaluate the following as an ECMAScript Program will help:

if (me.googlodyte)
{
with (me.brainSimulation)
{
if (navigator.go(
"http://groups.google.com/advanced_search",
"http://groups.google.com/",
i18n("Advanced Groups Search")))
{
if (navigator.paste(
navigator.copy(perception.currentPosting.body
.getNextEmailAddr()),
i18n("Lookup the message with message ID")))
{
if (navigator.click(i18n("Lookup Message")))
{
while (navigator.readyState != 4);
navigator.read();
}
}
}
}

while (!me.hasMinimumClue())
{
if (me.getInformedAbout("Usenet"))
{
me.getLocalNewsReaderApp();
}
}

me.googlodyte = false;
}
else
{
me.lookupMsgId(me.brain.perception.currentPosting.body.getNextMsgId());
}

Probably someone has told you this before.


PointedEars
 
A

Aaron Gray

Thomas 'PointedEars' Lahn said:
Yes, it only seems to be so.


Having yourself evaluate the following as an ECMAScript Program will help:

if (me.googlodyte)
{
with (me.brainSimulation)
{
if (navigator.go(
"http://groups.google.com/advanced_search",
"http://groups.google.com/",
i18n("Advanced Groups Search")))
{
if (navigator.paste(
navigator.copy(perception.currentPosting.body
.getNextEmailAddr()),
i18n("Lookup the message with message ID")))
{
if (navigator.click(i18n("Lookup Message")))
{
while (navigator.readyState != 4);
navigator.read();
}
}
}
}

while (!me.hasMinimumClue())
{
if (me.getInformedAbout("Usenet"))
{
me.getLocalNewsReaderApp();
}
}

me.googlodyte = false;
}
else
{
me.lookupMsgId(me.brain.perception.currentPosting.body.getNextMsgId());
}

Probably someone has told you this before.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

Have you not been told not to use the 'with' clause ?

Aaron
 
R

RobG

Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned by
you ?

He posted a version in this thread:

<URL:
http://groups.google.com.au/group/c...ying+javascript+error+thomas#c92963d7ec6dbc8b

A modified version of that is:

var isMethod = (function() {
var re = /\bfunction|object\b/;
return function(o) {
return re.test(typeof o);
}
})();


to me that is only marginally more effective than:

if (obj) obj();

or

obj && obj();


which at least saves looking up the isMethod function to see what it
does.

The definition of a function in javscript is an object that implements
the internal [[Call]] method[1], however there is no definitive test
for that, the closest is to actually call it within a try..catch block
and see what eventuates, though that can also be error-prone and
likely far more effort than it's worth in the vast majority of cases.

Using typeof should be sufficient for native functions, however host
functions do not have to return 'function' and don't in at least one
popular browser.

You might like this thread if you have an hour or so spare - there's a
lot to read! :)

<URL:
http://groups.google.com.au/group/c...is+a+function+not+a+function#617385810dfcdf9a

1. That isn't explicitly stated in ECMA-262 as far as I know, but can
be reasonably inferrred from sections 10.1.3 and 11.4.3
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top