简体   繁体   中英

Load JavaScript file source code as plain text and access its content using JavaScript

Is it possible to download a script file using the script tag like this

<script src="http://webserver.com/script.js"></script>

and then be able to read the loaded file content with JavaScript, like this for example:

var scriptContent = window.scripts[0].content;

What i came to notice is that the loaded JavaScript file contents is not accessible by other JavaScript files or am i wrong ?

No, it is not possible to do that.

The source code of a script loaded via src is not exposed via any API made available to JavaScript running in the page.

You could read the value of src and then fetch it using XMLHttpRequest .

The source code of specific functions may be available by calling toString() on them.

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