On-Demand javascript -
I am thinking that people can ask me on javascript using AJAX? If you can help me with a simple example, then I appreciate it.
My question is how to get the javascript code from my server and execute it?
If you really want to use AJAX (as mentioned in the question) you can also do this.
First of all, you normally download JavaScript using XMLHttpRequest and when the download is finished If you go, you insert it inside or a generated tag.
function xhr_load (url, callback) {xhr = new XMLHttpRequest (); Xhr.onreadystatechange = function () {if (xhr.readyState == 4) {if (xhr.status == 200) {callback (xhr.responseText); }}} Xhr.open ("GET", URL, true); Xhr.send (zero); } // (1) Download using XHR and execute eval () using xhr_load ('mylib.js', function {eval (response.responseText);}); (2) Download using XHR and execute an inline script xhr_load ('mylib.js', in the form of a function) {var script = document.createElement ('script'); Document.getElementsByTagName ('head') [0] .appendChild (script); Script.text = response.responseText;});
In addition to this, Steve Soders has done amazing work in this area and I can highly recommend watching it on my own.
Comments
Post a Comment