简体   繁体   中英

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>

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM