1

I know that in Jquery you can get the event.id if I had triggered an event (click, etc) but how would I get the DOM location of a simple function call

Ex :

<pre id="container2">...
  <script>
  FunctionCall();
  </script>
</pre>

I would like to get the "container2" value from inside my FunctionCall

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Erwald
  • 1,920
  • 1
  • 12
  • 20
  • var container2 = document.getElementById('container2'); ? – rissicay Dec 29 '12 at 00:06
  • or am i missing something? – rissicay Dec 29 '12 at 00:06
  • I need to get the location from within the FunctionCall without knowing it in advance... – Erwald Dec 29 '12 at 00:07
  • Simply to be able to add element to a page at specific places (where this function is been called). – Erwald Dec 29 '12 at 00:12
  • Well can't you pass it as an argument to FunctionCall() ? – Halim Qarroum Dec 29 '12 at 00:14
  • I would like this code to be really easy to use for the users so Id like not to pass it as an argument... – Erwald Dec 29 '12 at 00:20
  • I think you want to use `document.write`, you can read some [pros and cons about it here](http://stackoverflow.com/q/802854/417685). (If after reading there you still want to use it then I can post this as an answer) – Alexander Dec 29 '12 at 00:27
  • 1
    question is too vague... post more explicit details about what you are trying to do with more real world code example – charlietfl Dec 29 '12 at 00:49
  • 1
    @charlietfl - this question is not vague at all. He wants a called sub-routine to know where the call was made. This must exist because the browser's debugger can show the line number – Hogan Dec 29 '12 at 00:50
  • @Hogan a lot depends on how the method is constructed and how it is used – charlietfl Dec 29 '12 at 00:50
  • There must be a way to find out the line number of a prior function call in the stack since the debugger can show it. – Hogan Dec 29 '12 at 00:53
  • 1
    You should never need to know the position of an inline script. You need to change your design. – Beetroot-Beetroot Dec 29 '12 at 00:54
  • 2
    @Beetroot-Beetroot - Not true, I can think of a use case. The simple one, a logging framework for a system where you won't have access to the client's machine. – Hogan Dec 29 '12 at 00:55
  • @Beetroot-Beetroot How would you do it. The goal is kinda simple : add a specific text where ever FunctionCall is. This text might change depending on the position where the call FunctionCall is made. – Erwald Dec 29 '12 at 00:57
  • @Hogan, I'm struggling with that explanation. – Beetroot-Beetroot Dec 29 '12 at 00:57
  • Something inserts the script with the function call. Arrange for that something to insert whatever you really want there instead. If it's an issue at all, then it's a server-side issue. – Beetroot-Beetroot Dec 29 '12 at 01:00

2 Answers2

1

In general, the script does not know where it was launched from so there is no generic way to do what you were asking. You will have to either find a container div that you know in advance or insert your own known object that you can then find.

In your specific example, you could do this:

<pre id="container2">...
  <script>
  var fCntr = fCntr || 1;
  document.write('<div id="FunctionCallLocation' + fCntr + '"></div>');
  FunctionCall(fCntr++);
  </script>
</pre>

Then, from within the script, you can find the DOM element with the id that was passed to it.

Or, you could put the document.write() into the function itself so it marks its own location:

var fCntr = 1;
function FunctionCall() {
    var myLoc = "FunctionCallLocation" + fCntr++;
    document.write('<div id="' + myLoc + '"></div>');
    var myLoc = document.getElementById(myLoc);
}

This exact code would only work if FunctionCall was only called at page load time so the document.write() would work as desired.

jfriend00
  • 580,699
  • 78
  • 809
  • 825
-1

Maybe you could try add an id or class to the script element. So something like this:

<pre id='container2'>
    <script id='location'>
        (function FunctionCall() {
            var container2 = document.getElementById('location').parentNode;
            container2.appendChild(document.createElement('p'));
        }())
    </script>
</pre>
rissicay
  • 405
  • 3
  • 14
  • The problem with this is that if I have multiple – Erwald Dec 29 '12 at 00:18