views:

67

answers:

3

is it possible to use jQuery %Json function to load a javascriptfile.js asynchronously?

+4  A: 

$.getScript("/path/to/script.js")

Ramon
but what should the json response be?
scrippie
There shouldn't be a JSON response. You asked to load a script, not process a piece of JSON data.
David Dorward
i need the script.js embeded in the Json response, it gets a different file depending on response.
scrippie
the js file is part of the response
scrippie
That sounds decidedly dirty, but generate a script element and a text node containing the data extracted from the JSON. append the text to the script and the script to the body.
David Dorward
+1  A: 

If you're trying to load an external JavaScript file then you should use Ramon's suggestions.

But if you're for sure that the response format is JSON you could use the getJSON which loads JSON data using an HTTP GET request.

jQuery.getJSON( url, [data], [callback] )
SpartanBeg
A: 

Here's pure JavaScript coding that downloads a JavaScript file and put it in the header page itself (if you plan to have a very lightweight page that is instantaneously visible then download JavaScript file in the background) :

var getheadTag = document.getElementsByTagName('head')[0];
setjs = document.createElement('script');
setjs.setAttribute('type', 'text/javascript');
getheadTag.appendChild(setjs);
setjs.text = x.responseText;
Olivier Pons