How might one go about obtaining a reference to the DOM script tag that called a function?
For example:
<script type="text/javascript">
var globalfunc = function(){
//obtain a reference to the script block that globalfunc() was called from
}
</script>
<script type="text/javascript">
globalfunc();
</script>
I know that I could assign an id or class to the script element and pass that through to my function to do a DOM lookup, but I think it would be more flexible and elegant to do it without such a thing. The main reason is that I may have to do some work on our site to get 3rd party ads wrangled into shape, and we don’t have control over the scripts they use to load their ads.
Actually, you can’t assign a class or id to a
scriptelement if you want to have valid markup. That said, I have used that method and yes it does work.Alternatively, and let the record show that I would never do this, you could do something like: