tags:

views:

25

answers:

1

Example:

<script>
// I want to have the dom element of the script tag that is holding the code that goes here
</script>

If I do:

<script>
alert(this)
</script>

this is the window.

I want to access the script tag itself.

A: 

This should find the <script> elements you have in your page.

var scripts = document.getElementsByTagName("script");

for (var i = 0; i < scripts.length; i++) {
    var script = scripts[i];
    // Now you can do stuff with script
}

If you want the one the code is in I think you can do this:

var scripts = document.getElementsByTagName("script");
var script = scripts[scripts.length - 1];

Just curious: what do you want to do with it once you've got it?

Cory Larson
No, it's not a joke. The second part of the answer is not iterating over all the scripts. Do you want the CODE or the script DOM element?
Cory Larson
Nevermind and sorry. I got you know.