0

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?

2 Answers 2

1

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

@MohmmadEbrahimiAval I'm glad to help you. If this is what you are looking for, consider accepting my answer.
you know ,how stop and delete after executed ?
@MohmmadEbrahimiAval No need for it. It automatically gets removed.
0

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
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.