1

Is that possible to know where is a jQuery function launched from ?

<div>
  <script language="javascript">
    $(document).ready(function () {
        whereAmI($(currentDIV));
    });
  </script>
</div>
user1288337
  • 135
  • 1
  • 2
  • 7
  • Are you searching for: "this"? (in your example "this" would be "document"). To get a jquery object, use: $(this). – Thomas Kekeisen Mar 23 '12 at 17:55
  • I think you are seraching for what trigged the function? For example you want to know that whereAmI was trigged by the document ready function? – Simon Edström Mar 23 '12 at 17:58
  • See [How may I reference the script tag that loaded the currently-executing script?](http://stackoverflow.com/questions/403967/how-may-i-reference-the-script-tag-that-loaded-the-currently-executing-script) – calebds Mar 23 '12 at 18:00
  • It can drawed like that:
    I actually don't know the div ids
    – user1288337 Mar 23 '12 at 18:03

2 Answers2

0

If the function is being called by some sort of event, the first parameter is the Event object and you can just write something like.

$('a').click(function(e){
    console.log(e.target);
});
mordy
  • 1,035
  • 9
  • 6
  • I meant that the whereAmI() function will be launched when DOM is ready but I want to localize the script itself in page. Saying differently, whereAmI() function can be iterated in many parts of my webpage as the div container is a ajax callback. This jQuery function will prepare handlers in this div. I actually don't want to copy/paste my whereAmI() function content in each partialview... – user1288337 Mar 23 '12 at 18:02
0

There is nothing saved for you about the DOM location whence the currently executing JavaScript was loaded. However, you can use the fact that scripts are executed as they are loaded to achieve this memory:

<div id="target">
  <script>
      var thisScript1 = $('script').filter(':last');
      $(document).ready(function () {
          console.log(thisScript1.closest('div').attr('id')); // prints 'target'
      });
  </script>
</div>
<div id="second">
  <script>
      var thisScript2 = $('script').filter(':last');
      $(document).ready(function () {
          console.log(thisScript2.closest('div').attr('id')); // prints 'second'
      });
  </script>
</div>​

Here, $('script').filter(':last'); refers to the last script element on the page, which at the time of execution is the "current script". Note that for each script I want to remember, I've named the variable housing the script object differently, otherwise it will be clobbered by subsequent scripts. An alternative would be to wrap each script in an immediately executed anonymous function, for the luxury of binding a local thisScript variable.

Community
  • 1
  • 1
calebds
  • 25,670
  • 9
  • 46
  • 74
  • High five for you. I'll play around your tips here see how it can handle my needs. Thanks to you guys all for your help. – user1288337 Mar 23 '12 at 18:42
  • Sorry it can not work. I've just tested and it conditionned by the uniqueless var name (thisScriptx)... Otherwise it will be overwriten by the last javascript call and all function call will be with the same argument value :( – user1288337 Mar 23 '12 at 21:36