Log in

View Full Version : [JavaScript] Getting a JavaScript file with AJAX and running it



nets
12-07-2006, 03:19 PM
Okay, there's three parts to this script; one part which gets the correct request for the client's browser, another which makes the request and another which waits and then handles the output.

To get the correct request we'll check if "window.XMLHttpRequest" returns true, if it does then this function is available and we'll use it; if it returns false then we'll assume the client's browser is Internet Explorer, and therefore create the correct request object for Internet Explorer.


if (window.XMLHttpRequest) {

var request = new XMLHttpRequest();

} else {

var request = new ActiveXObject('Microsoft.XMLHTTP');

}

Now we've got the correct request object, we'll create a new GET request to retrieve our JavaScript file. Under the request is an event, which will call a function (which handles the output) once a change is detected.


function getJS() {

request.open('get','script.js');

request.onreadystatechange = runJS;

request.send(null);

}

The following script checks that the request is on currently on the correct state (where it has retrieved all our script), once the correct state is met we use the "eval()" function to run the response text string.


function runJS() {

if(request.readyState == 4) {

eval(request.responseText);

}
}

Bellow is a final example of the script, and I've supplied a link to a working example underneath.


<SCRIPT TYPE="TEXT/JAVASCRIPT">
<!--



if (window.XMLHttpRequest) {

var request = new XMLHttpRequest();

} else {

var request = new ActiveXObject('Microsoft.XMLHTTP');

}




function getJS() {

request.open('get','script.js');

request.onreadystatechange = runJS;

request.send(null);

}




function runJS() {

if(request.readyState == 4) {

eval(request.responseText);

}
}




document.write('Get & run JS'.link('javascript:getJS();'));


-->
</SCRIPT>

http://joshjh.pwp.blueyonder.co.uk/AJAXExample.htm

DMB-Hosting
12-07-2006, 06:50 PM
I started learning ajax a few weeks ago

---MAD---
12-07-2006, 07:11 PM
whats it used for though?

nets
12-07-2006, 07:30 PM
AJAX? You can get/send data without the user's page reloading.

DMB-Hosting
12-07-2006, 08:09 PM
Yeah saves bandwidth too, very usefull

Mentor
13-07-2006, 08:40 PM
Can you realy learn ajax? since technicly its more a technique you do with javascript that something in itself.

Also before the ajax indies come in technicaly its not ajax since it doesnt use the code asynconusly and it doesnt work with xml "/

nets
13-07-2006, 11:05 PM
Can you realy learn ajax? since technicly its more a technique you do with javascript that something in itself.
He might have been referring to learning the technique.

timROGERS
15-07-2006, 02:13 PM
I wouldn't use AJAX as a way to save bandwidth, because I won't save much at all.

Mentor
15-07-2006, 11:47 PM
I wouldn't use AJAX as a way to save bandwidth, because I won't save much at all.
much at all? lets say you have a big page.
86 kb for my useless info archive. Now u want people to vote on a fact, You can go normal, vote, then transfer another 86kb page. or u can sent a tiny ajax request, And take less than a kb to do so... thats a VERY big saveing...

---MAD---
18-07-2006, 11:20 AM
I wouldn't use AJAX as a way to save bandwidth, because I won't save much at all.
Some people over do it however if you use it in the right places, its VERY helpful.

Also read mentors post ;)

Want to hide these adverts? Register an account for free!