element?

Can JavaScript get the source code for the <script src = ""> element?

<script id="s1" src="foo.js"></script> <script> alert('foo.js contains' + _source_code_of('s1')) </script> 

Can _source_code_of be implemented?

+8
javascript html ajax
source share
2 answers

No, this will allow you to retrieve the contents of any URL, which violates some security policies. (This will be the equivalent of an ajax get request without checks on the same domain.)

However, since foo.js is in the same domain as the page, you can get it using an ajax request. JQuery example:

 $.get('foo.js', function(source_code) { alert('foo.js contains ' + source_code); }); 
+9
source share

No, not directly for fundamental security reasons.

The fact that you noted this with Ajax implies that you are trying to use this as a way to get data. If so, the closest similar approach is JSONP, in which a newly loaded script calls a method to pass data back to the parent document.

+1
source share

All Articles