tags:

views:

1029

answers:

12

If I have a script tag like this:

<script
    id = "myscript"
    src = "http://www.example.com/script.js"
    type = "text/javascript">
</script>

I would like to get the content of the "script.js" file. I'm thinking about something like document.getElementById("myscript").text but it doesn't work in this case.

+3  A: 

You want to get the contents of the file http://www.example.com/script.js ? If so, you could turn to AJAX methods to fetch its content, assuming it resides on the same server as the page itself.

Could you elaborate on what you're trying to accomplish?

_Lasar
I want to store the content of the script so that I can cache it and use it directly some time later without having to fetch it from the external web server (not on the same server as the page).
Markus Johansson
If so, don't worry about that, the browser handle itself the caching of external JS scripts.
gizmo
A: 

Not sure why you would need to do this?

Another way round would be to hold the script in a hidden element somewhere and use Eval to run it. You could then query the objects innerHtml property.

alexmac
+1  A: 

if you want the contents of the src attribute, you would have to do an ajax request and look at the responsetext. If you where to have the js between and you could access it through innerHTML.

This might be of interest: http://ejohn.org/blog/degrading-script-tags/

olle
A: 

If you're looking to access the attributes of the <script> tag rather than the contents of script.js, then XPath may well be what you're after.

It will allow you to get each of the script attributes.

If it's the example.js file contents you're after, then you can fire off an AJAX request to fetch it.

ConroyP
A: 

The question is not clear enough. Do you want to get the content between the tags (which is empty in your example) or the contents of the script file (source)?

Cd-MaN
+1  A: 

If a src attribute is provided, user agents are required to ignore the content of the element, if you need to access it from the external script, then you are probably doing something wrong.

Update: I see you've added a comment to the effect that you want to cache the script and use it later. To what end? Assuming your HTTP is cache friendly, then your caching needs are likely taken care of by the browser already.

David Dorward
+1  A: 

.text did get you contents of the tag, it's just that you have nothing between your open tag and your end tag. You can get the src attribute of the element using .src, and then if you want to get the javascript file you would follow the link and make an ajax request for it.

toby
+2  A: 

I don't think the contents will be available via the DOM. You could get the value of the src attribute and use AJAX to request the file from the server.

Dan Goldstein
A: 

In a comment to my previous answer:

I want to store the content of the script so that I can cache it and use it directly some time later without having to fetch it from the external web server (not on the same server as the page)

In that case you're better off using a server side script to fetch and cache the script file. Depending on your server setup you could just wget the file (periodically via cron if you expect it to change) or do something similar with a small script inthe language of your choice.

_Lasar
A: 

Using 2008-style DOM-binding it would rather be:

document.getElementById('myscript').getAttribute("src");
document.getElementById('myscript').getAttribute("type");
roenving
A: 

Hopefully you are already using some JavaScript library...

What about getting the src attribute's value, the URL, and then use your library's Ajax tools to make a request to that URL and save that result wherever you are desiring to do so?

The specific details would vary depending on the library you are using.

Jason Bunting
A: 

You want to use the innerHTML property to get the contents of the script tag:

document.getElementById("myscript").innerHTML

But as @olle said in another answer you probably want to have a read of: http://ejohn.org/blog/degrading-script-tags/

Sugendran