admin管理员组

文章数量:1335401

I am trying to get content of <script> tag in html:

i can get <script>test</script> content by using jquery $("script").html().

but how to get content of <script src='something.js'></script> ?

I am trying to get content of <script> tag in html:

i can get <script>test</script> content by using jquery $("script").html().

but how to get content of <script src='something.js'></script> ?

Share Improve this question asked Apr 22, 2013 at 5:01 Chito AdinugrahaChito Adinugraha 1,3202 gold badges16 silver badges22 bronze badges 1
  • 3 Possible duplicate of How can I get the content of the file specified as the 'src' of a <script> tag? and also possible dup of Can JavaScript access source code of a <script src=“”> element? – jfriend00 Commented Apr 22, 2013 at 5:03
Add a ment  | 

3 Answers 3

Reset to default 5

What about this.

$.get("/something.js",function(scriptContent){alert(scriptContent)});

It will pull the content through an ajax request

Alternatively, you can pull the content on the server and return it back to the client by making a request to a page that facilitates the loading of the JS file on the server. This is necessary if the js file is hosted under a different domain

You can fetch the contents of a script file with ajax if it es from the same domain as your page.

If you can explain what problem you're really trying to solve, we can likely give you better options.

You can get the content of javascript files (or any text file for that matter) using Ajax. Either use XMLHttpRequest then read the responseText or load it in an iframe.

Be warned that both methods are limited by the same origin policy.

本文标签: javascriptGetting content of ltscriptgt tagStack Overflow