I’m trying to execute JavaScript functions that are called when a event (for example onClick event) is performed on a web page with JavaScript code. I’m getting the function from the event like this :
var attributval = document.getElementsByTagName("a").getAttribute('onClick');
and I’m trying to execute this object (which a JavaScript function in fact) as a function (suppose we have <a onClick = alert('whatever');> on this example, I tried:
var attributval = document.getElementsByTagName("a").getAttribute('onClick');
attributval() = function(){attributval};
attributval();
but it didn’t work.
A DOM attribute is not the same as a JavaScript property (even though they can have the same name
onclick). You should useto retrieve a function (or
null) from the JS object (as opposed togetAttribute(), which will most likely return atoString()for the property).Now,
attributval() =is illegal syntax, asattributval()is not an l-value (you cannot assign to it).attributval();will work but without the second line (which is illegal JavaScript) it will invoke the original A elementonclickhandler (if one is defined) or throw an exception (if theonclickhandler isnull).