<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta name="generator" content="BBEdit 9.6">
<script type="text/javascript">
window.onload = function() {
document.getElementById('item')['onclick'] = (function() {
var x = function() { alert('Clicked'); };
x();
return x;
})();
};
</script>
</head>
<body>
<div id="item" style="width:20px;height:20px;background-color:red;"></div>
</body>
</html>
What I would like to know is if there is a way to simplify it and return a reference to the function from within the function itself? Obviously this doesn’t work as shown below.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta name="generator" content="BBEdit 9.6">
<script type="text/javascript">
window.onload = function() {
document.getElementById('item')['onclick'] = (function() {
// alert once and after whenever the item is clicked
alert('Clicked');
return this; // this is the window object - is there a way to reference the function definition?
})();
};
</script>
</head>
<body>
<div id="item" style="width:20px;height:20px;background-color:red;"></div>
</body>
</html>
The practical scenario is I have several functions attached to the jQuery ajaxSucess event. Each function needs to execute once during initial load and after when the JQuery ajaxSuccess event is fired. So I am just looking to see if I can eliminate a a few lines of code and learn something new in the process, that is really all.
window.onload = function() {
document.getElementById('item')['onclick'] = (function() {
// alert once and after whenever the item is clicked
alert('Clicked');
return arguments.callee; // this is the window object - is there a way to reference the function definition?
})();
};