8

I would like to collect all the <script> ....</script> code section present in the HTML page in some variable.

What should be the simpler way to do this, Any idea how it can be retrieved using JavaScript.??

Any help will be greatly appreciated.

4
  • 3
    What problem are you trying to solve? Jun 18, 2012 at 7:39
  • You mean using a JavaScript script, aren't you?
    – sp00m
    Jun 18, 2012 at 7:41
  • Possible duplicate stackoverflow.com/questions/3160609/…
    – Anand
    Jun 18, 2012 at 7:43
  • @SimeonVisser with the script variable containing all scripts I want to show the links of the scripts to github on my [website](tik9.github.io) to show the users how I use scripts on my site.
    – Timo
    Dec 27, 2021 at 10:13

6 Answers 6

36

To get a list of scripts you can use

  • document.getElementsByTagName("script"); by tag
  • document.scripts; Built-in collection
  • document.querySelectorAll("script"); by selector
  • $("script") jQuery by selector

var scripts = document.getElementsByTagName("script");
for (var i = 0; i < scripts.length; i++) {
  if (scripts[i].src) console.log(i, scripts[i].src)
  else console.log(i, scripts[i].innerHTML)
}

// To get the content of the external script 
// - I use jQuery here - only works if CORS is allowing it

// find the first script from google 
var url = $("script[src*='googleapis']")[0].src; 

$.get(url,function(data) { // get the source 
  console.log(data.split("|")[0]); // show version info
});  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
  console.log("Inline script");
</script>
<script>
  function bla() {
    console.log("Other inline script");
  }
</script>

11

The simplest way is probably document.scripts

1

You would do:

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

Now scripts is a NodeList (like an array), and you can access each one using scripts[0], scripts[1] and so on.

1

try this

var scripts = document.getElementsByTagName("script");
2
  • 1
    It does not return an Array
    – Dev555
    Jun 18, 2012 at 8:04
  • Typing or spelling mistake. It is getElements, not getElement, but it has been answered in depth elsewhere on this page too :)
    – mplungjan
    Jun 18, 2012 at 8:29
0

Without jQuery :

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

With jQuery :

var scripts = $("script");
0
0

Here you go --

(function () { 
        'use strict';
        let logscript = function () {
            let js = document.scripts;
            for (let i = 0; i < js.length; i++) {
                if (js[i].src) {
                        console.log(i, js[i].src);
                    } else {
                        console.log(i, js[i].innerHTML);
                }   
            }
        };
        if (document.readyState === 'complete') {
                logscript();
        } else {
                window.addEventListener('load', logscript);
        }
})();

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.