How to get a Script Tag's innerHTML

Alright... I've been searching for an hour now... How does one get the innerHTML of a script tag? Here is what I've been working on...

<script type="text/javascript" src="http://www.google.com" id="externalScript"></script>
<script type="text/javascript">
function getSource()
 {document.getElementById('externalScript').innerHTML;
  }
</script>

I've been trying to work on a way to call another domain's page source with the script tag. I've seen a working example, but cannot find it for the life of me...

Answers:

Answer

You can't do that. There is no innerHTML....all you can do is pull down the file view XMLHttpRequest to get to its contents....but of course, that is limited by same-origin policy, but script tags are not. Sorry.

Answer

actually, there is a way to get the content, but it depends on the remote server letting you get the file without valid headers and still fails a lot of the time just because of those settings. using jQuery since it's the end of my day and I'm out the door....

$.get($('#externalScript').attr('src'), function(data) {
    alert(data);
});
Answer

I'm guessing you want one of two things:

  1. To make a JavaScript file global (so that other pages can call it)

  2. To get the script that is currently in the file

Both of those can be solved by moving your script to a .js file, and then using the tag

<script src="[path-to-file]"></script>
Answer

You can't do this. It would be a massive security problem if you could.

Script content can include any number of things. Consider this: a script loaded from a URL on your bank's website might contain all sorts of things, like your account number, your balance, and other personal information. That script would be loaded by your bank's normal pages to do what they want to do.

Now, I'm an evil hacker, and I suspect you may be a customer of Biggo Bank. So on one of my own pages, I include a <script> tag for that Biggo Bank script. The script may only load if there's a valid Biggo Bank session cookie, but what if there is? What if you visit my hacker site while you're logged in to Biggo Bank in another browser tab? Now my own JavaScript code can read the contents of that script, and your money is now mine :)

Answer

You can Use Html Parsers:

jsoup » jsoup: Java HTML Parser jsoup: Java HTML Parser

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

refer this: http://jsoup.org/

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.