What is the current element in Javascript? [duplicate]
Asked Answered
C

4

12

How can I find out what the element is that a <script> sits in?
As an example, let's take this

<div>
 <script type="text/javascript">
  var time = new Date(), hrs = time.getHours(), min = time.getMinutes();
  document.write('It is '+hrs+":"+(min<10?'0':'')+min);
 </script>
</div>

Then if I want to change this to something more modern, how can I find out what element we're in?
So I want to write, for instance in jQuery

$(thisdiv).html('It is '+hrs+":"+(min<10?'0':'')+min);

but how do I get thisdiv?
Yes, I know, I can put an ID on it, but I have the feeling that wouldn't be necessary. The browser knows where we are, otherwise it couldn't even do the document.write!

So, suggestions? I searched, but couldn't find it. Is it so simple that I'm overlooking the obvious?

Crocein answered 9/2, 2012 at 11:11 Comment(0)
M
30

Script are executed in the order of appearance in the document. The contents of a script tag are evaluated on encounter, so, the last <script> element is always the current one.

Code:

<div>
  <script>
    var scriptTag = document.getElementsByTagName('script');
    scriptTag = scriptTag[scriptTag.length - 1];

    var parent = scriptTag.parentNode;
  </script>
</div>
Maddie answered 9/2, 2012 at 11:25 Comment(4)
+1, I can't believe I overlooked that rather fundamental concept :(Yajairayajurveda
Thank you! I wasn't aware either that the latest script was the last in the list, during its execution, so this the solution I was looking for. And as a plus, it works on all browsers I tested on, even Netscape 6. (Yeah, I'm thorough like that.)Crocein
Wow, good one, hadn't thought about that.Fari
Great to know. Useful in CMS where you don't want to add an id to a component to prevent from duplicates but still want to initialize it using a plugin. Like on a blog where multiple posts are shown. In jQuery one can use $('script').last().Dispatcher
E
3

Firefox:

document.currentScript.parentElement

Chrome:

document.scripts.length
Erwin answered 9/2, 2012 at 11:29 Comment(1)
Currently all browsers (except IE) support document.currentScript (see Link )Manufactory
M
0
$('script#some_id').parent().html('blah blah');
Maladapted answered 9/2, 2012 at 11:15 Comment(2)
Yes, I know, I can put an ID on it, but I have the feeling that wouldn't be necessary.Dapplegray
Mr Lister did mention not giving the script an id, previously on SO I was looking for a similar solution because I have many script els in a document which all required a knowledge of "where they are", the best I could come up with was setting a uniquely random var in the script block for identifying access within other scripts (#3058128). So maybe id's are the way to go... eagerly watching your Q.Yajairayajurveda
G
0

Try this

<div id="mydiv">
 <script type="text/javascript">
  var time = new Date(), hrs = time.getHours(), min = time.getMinutes();
  document.getElementById('mydiv').innerHTML = 'It is '+hrs+":"+(min<10?'0':'')+min;
 </script>
</div>
Gastrulation answered 9/2, 2012 at 11:23 Comment(2)
Yes, I know, I can put an ID on it, but I have the feeling that wouldn't be necessary.Dapplegray
oh sorry i had not got that statement, i think Mr Rob W is quite nearer to your solutionGastrulation

© 2022 - 2024 — McMap. All rights reserved.