How to Get the Content of the File Specified as the 'Src' of a <Script> Tag

How can I get the content of the file specified as the 'src' of a script tag?

Do 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.

Getting content of script tag

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

Getting content of a script file using Javascript

I think what you want to do is to assign a variable inside template.js. Then you have the variable available for use wherever you want in jquery. Something like:

var tpl = "<div> ... </div>"

Wouldn't this be a simpler solution to your problem? We do this in Ext JS. I think this will work for you in jQuery.

Is it possible to access script tag content when SRC'ed?

From MDN,

Script tag

This attribute specifies the URI of an external script; this can be used as an alternative to embedding a script directly within a document. script elements with an src attribute specified should not have a script embedded within its tags.

Get the content of a cross domain script tag

If you attempt to include your custom script directly via a tag then it will fail to parse, and you won't be able to get the 's innerHTML.

I would suggest using a JSONP-style approach. Start by defining a global handler function, e.g.

// myproglang.js

window.registerSource = function(source) {
myProgLang.eval(source);
}

And then in your source file, wrap your code in a call to that function:

// source.mypl
registerSource("\
Dim foo as Integer;\
")

It's a bit ugly as it requires the text file to escape newline characters in order to maintain the string literal, but other than using XHR, that's the only viable solution I can think of.

You could always write a compile step for this "language" that replaces "\n" with "\\\n" and wraps the source in a registerSource() call. As long as it's transparent it's a viable option.

Get content inside script as text

Just give your script tag an id:

<div></div>
<script id='script' type='text/javascript'>
$('div').html($('#script').html());
</script>

http://jsfiddle.net/UBw44/

How do I read JS as data from a script tag?

I may not exactly understand what is it and why you want to implement this. Considering that you do not want to use ajax due to slow server issues, you might as well do it old school.
If your page is not very heavy, you can -

  • Put a hidden iframe on the page pointing its src to your JS file.

  • Wait for the $('document').ready() to be called inside the iframe, i.e. let the iFrame load all the content.

  • Copy the contents of the iframe one its loaded into the HTML element container you want.

Hope this helps!



Related Topics



Leave a reply



Submit