Mohmmad Ebrahimi Aval
Mohmmad Ebrahimi Aval

Reputation: 482

how to get .js file context with jQuery $.ajax without execute when receive

when get *.js file by $.ajax, Scripts are executed after receive!

how to get and executed when i want? AND how to destroy this scripts when i want?

Upvotes: 0

Views: 95

Answers (2)

Mohmmad Ebrahimi Aval
Mohmmad Ebrahimi Aval

Reputation: 482

i found answer!!

use 'converters' on $.ajax! e.g. :

$.ajax({
    url: "test.js",
    converters: {
        'text script': function (text) {
            return text;
        }
    },
    success: function(scripts) {
        //do something
    }
});

Upvotes: 0

user6586783
user6586783

Reputation:

Not sure for jQuery, but you can do it using XHR method.

function getScript(b, c){
  var a = new XMLHttpRequest();
  a.open('get', b, true);
  a.onreadystatechange = function(){
    a.readyState != 4 || a.status && a.status != 200 || c(a.responseText);
  };
  a.send();
}

getScript('script.js', function(content){
    // Do whatever here
    Function(content)(); // Execute the script
});

Upvotes: 1

Related Questions