About anonymous function:
Interestingly enough, both IE and FF (but not Opera) have two different
string representations for anonymous functions:
1) "stay-alone" function via Function() or an intrinsic handler:
"function anonymous().."
2) a method of an object:
"function() ..."
Opera always has the 2nd variant.
So if my getFunctionName returns empty string and it's not Opera, you
can be sure that you've got a reference to an object method.
P.S. The code below tested with The NSCA Mosaic and rendered properly.
P.P.S. The only site I've found so far that would be viewable and
operable with Mosaic is
www.w3.org. No wonder their site is the ugly as
a sin. :-)
<html>
<head>
<title>Functi on name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
<!--
function ObjectConstruct or() {
this.method = function(){aler t('ocMessage'); }
}
var oInstance = new ObjectConstruct or();
var fInstance = new Function("alert ('fInstanceMess age');");
function test() {
//var isOpera = (navigator.user Agent.indexOf(' Opera') != -1);
alert(getFuncti onName(argument s.callee)); // 'test'
alert(getFuncti onName(fInstanc e)); // 'anonymous' || '' (Opera)
alert(getFuncti onName(ObjectCo nstructor)); // 'ObjectConstruc tor'
alert(getFuncti onName(oInstanc e.method)); // true
}
function getFunctionName (f) {
fName = '';
var fName = '';
var fCode = f.toString();
var re = /(function\s*)(\ w+)(\s*\()/;
if (re.test(fCode) ) {
re.exec(fCode);
fName = RegExp.$2;
}
return fName;
}
window.onload = test;
//-->
</script>
</head>
<body>
<noscript>
<h4>JavaScrip t is not supported or turned off.
This page may not operate as being designed.</h4>
</noscript>
</body>
</html>