House3272
House3272

Reputation: 1037

Javascript: is it possible to read the raw code of external scripts?

Given a typical script tag:

<script src="foo.com/myscript.js"></script>

would it be possible to directly read the contents of myscript.js as a string or something?
For example:

<script id="myscript" src="foo.com/myscript.js"></script>
<script>
    var inners = document.getElementById("myscript").//raw contents of myscript.js
</script>

Upvotes: 1

Views: 320

Answers (1)

Amadan
Amadan

Reputation: 198476

No. You can read the contents of the inline script tag, because it actually does have content:

<script id="myscript">
  var inners = document.getElementById("myscript").textContent;
</script>

But for the external JS, the script contents are not actually put into the DOM; you would need to re-fetch it using AJAX (it would normally be cached unless anti-caching measures were taken, so you would not really take much time to re-fetch).

Upvotes: 2

Related Questions