0
<script>
$.getScript("javascriptfile.js", function(data ){


   alert("Script loaded and executed."+data);


});
</script>

data return undefined. I want to get the response data or content of javascriptfile.js

tested below not working

$.getScript("http://code.jquery.com/jquery-1.9.1.min.js", function(data){


   alert("Script loaded and executed."+data);


});
cometta
  • 35,071
  • 77
  • 215
  • 324

3 Answers3

5

There's a problem

//This one will NOT work
$.getScript("http://jquerymobile.com/branches/popup-widget/js/", 
        function (data) {
               alert("Script loaded and executed." + data);
});

//This one will work
$.getScript("js/jquery.min.js", 
        function (data) {
               alert("Script loaded and executed." + data);
});

$.getScript() doesn't provide content of the script file if you are making a cross origin request outside of your domain.

yogi
  • 19,175
  • 13
  • 62
  • 92
2

Kindly run this and reply with the exception message, so I can follow up with you:

$.getScript("javascriptfile.js")
.done(function(script, textStatus) {
    alert(textStatus);
})
.fail(function(jqxhr, settings, exception) {
    alert(exception);
});
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
0

i don't know why you'd want the content of the file since getScript executes it after loading it & the call back is just a callback to if necesary call functions/plugins/whatever you have just loaded; if you want content of a file do $.get instead but anyway

do 
function(data, textStatus, jqxhr) {
    console.log(data); //data returned
    console.log(textStatus); //success
    console.log(jqxhr.status); //200
    console.log('Load was performed.');
  });

to spot the "problem" if any (from http://api.jquery.com/jQuery.getScript/)

mikakun
  • 2,203
  • 2
  • 16
  • 24
  • does not work. i already tried this before post – cometta Feb 07 '13 at 08:07
  • i didn't write it would work but that it would narrow down the problem, paste your console results (in fact me using with .done & the cachedscript example provided in the link given; if any error a typo/incorrect url... – mikakun Feb 07 '13 at 08:25