Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q536 How does a function know which object called it?

You are here: irt.org | FAQ | JavaScript | Misc | Q536 [ previous next ]

A function can find out which function called it using the caller property:

<script language="JavaScript"><!--
function x() {
    alert('function x() was invoked by function ' + x.caller);
}

function y() {
    x();
}

y();
//--></SCRIPT>

A function can be easily passed the object in question using "this":

<script language="JavaScript"><!--
function test(what) {
    alert(what.href);
}
//--></script>

©2018 Martin Webb