1

I'm trying to get the html / inner content / text of all the script tags that are loaded on the page. Here is some sample code:

<script name="test1">
    alert('hi');
</script>
<script src="https://example.com/js.js name="test2""></script>

When I include jQuery, and run the following, I can't seem to get access the content of the remote script:

alert( jQuery('script[name="test2"]').html() )

Any thoughts on why that is?

onassar
  • 3,313
  • 7
  • 36
  • 58
  • 3
    because there is no content inside of the tag. The browser does not inject it into the page, it is evaluated. – epascarello Oct 19 '18 at 13:05
  • 3
    i think you can get whole inner content of script by using ajax try to request with that src and get response of content of that script – Rahul Dudharejiya Oct 19 '18 at 13:05
  • The syntax on the second `script` tag is broken. You've put the quotes in the wrong place – Rory McCrossan Oct 19 '18 at 13:06
  • Answers are horrible with this, but it is a dupe: https://stackoverflow.com/questions/148441/how-can-i-get-the-content-of-the-file-specified-as-the-src-of-a-script-tag – epascarello Oct 19 '18 at 13:07
  • 1
    scripts don't have HTML inside them, by definition. And yes, ones which link to an external source definitely don't, the code is just fetched and evaluated, it doesn't become part of the DOM. But why do you want to, anyway? – ADyson Oct 19 '18 at 13:10
  • Ahh okay yes I just saw that question. Thanks! – onassar Oct 19 '18 at 13:11
  • 1
    Possible duplicate of [How can I get the content of the file specified as the 'src' of a – onassar Oct 19 '18 at 13:12

0 Answers0