Get Caller Function Name
a function to get the calling functions' name. Is as safe as possible and attempts to read the whole function if needed to get it.
by bladnman
HTML
<script src="https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js"></script>
<input type=button id="theButton" value="run test" class="runButton">
<div id="log" class="log"></div>
CSS
.runButton {
width: 125px;
margin: 20px;
}
.log {
padding:10px;
margin: 20px;
border: 1px dotted #ccc;
color:#888;
font-face: arial;
font-size:12px;
background: #fbfbfb;
}
JavaScript
/* ************************************ */
function runTest() {
debug();
debug("runTest caller name", callerName());
callMe();
}
function callMe() {
debug("callMe caller name", callerName());
}
function callerName() {
try {
var myCallee = arguments.callee;
var hisCallee = myCallee.caller.arguments.callee;
var hisCallerName = hisCallee.caller.name;
if (isNoE(hisCallerName)) {
var hisCallersFunction = hisCallee.caller.toString();
if (!isNoE(hisCallersFunction)) {
hisCallerName = fBetween(hisCallersFunction, "function", "(");
}
}
hisCallerName = trim(hisCallerName);
}
catch (ex) {
hisCallerName = "";
}
if (isNoE(hisCallerName)) {
return "(anonymous)";
}
return hisCallerName;
}
/* ************************************
_ _ ___ _ ___ ___ ___ ___
| || | __| | | _ \ __| _ \/ __|
| __ | _|| |__| _/ _|| /\__ \
|_||_|___|____|_| |___|_|_\|___/
************************************ */
function trim(inString) {
return inString.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function getStringValue(inString) {
if (inString == null || inString == "undefined" || inString == "null" || inString == "[object]" || inString == "[object NodeList]") {
return "";
}
try {
var tString = new String(inString);
return tString.toString();
} catch (e) {
return "";
}
}
function fLeft(inText, delim) {
inText = getStringValue(inText);
delim = getStringValue(delim);
var outText = "";
var theSpot = inText.indexOf(delim);
if (theSpot > -1) {
outText = inText.substring(0, theSpot);
}
return outText;
}
function fLeftBack(inText, delim) {
inText = getStringValue(inText);
delim = getStringValue(delim);
var outText = "";
var theSpot = inText.lastIndexOf(delim);
if (theSpot >...