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="" 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...
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." 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...
Share Improve this question asked Nov 8, 2010 at 23:17 YoshiyahuYoshiyahu 411 silver badge2 bronze badges 5-
Do you want to have an
iframe
? The first line of your code is definitely not right...<script>
is for including e.g. JavaScript, not HTML. – Felix Kling Commented Nov 8, 2010 at 23:22 - I do not think that you can use id attribute with script tag, at least w3schools say that script does not support any standard attributes w3schools./tags/tag_script.asp – Olga Commented Jan 20, 2012 at 8:58
- @Olga I have been told several times not to refer to W3schools. due to lack of credibility and security. – CMS_95 Commented Oct 28, 2013 at 12:52
- @CS_STEM I guess I was mislead myself =) w3/html/wg/drafts/html/master/… according to spec, global attributes are ok at least in html5. But I'd still wouldn't want to tie my scripts (javascripts) to tags (script tag) via id, it will proove inconvinient when you need to move this or that. – Olga Commented Oct 28, 2013 at 13:37
- @Olga I should have said do not refer to it on stack overflow because some people don't like it for reasons but I would still encourage using it for quick tag references and code snippets because some times it is helpful. – CMS_95 Commented Oct 31, 2013 at 0:06
5 Answers
Reset to default 3You 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.
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);
});
I'm guessing you want one of two things:
To make a JavaScript file global (so that other pages can call it)
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>
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 :)
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/