Jump to content

Possible to run AJAX w/o getting a file from server?


retro-starr

Recommended Posts

I was reading a tutorial for AJAX after I read another for javascript and thought, what if you could use void(0) in the URL part of AJAX so you can get a dynamic local page. AJAX reloads just a certain part of whatever you want, but it gets a file from a server. Am I thinking about this the right way?

Link to comment
Share on other sites

I actually don't, it was more of a thought experiment. The intended code would look something like this in my opinion:

function loadXMLDoc(){if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    CODE HERE    }  }xmlhttp.open("post","void(0)",true);xmlhttp.send();

That's about as much of an example I would know as I still am not 100% with my studies of AJAX. The main thought I had was using AJAX to make a file manager with dual panes where AJAX could dynamically load the folders in each pane.

Link to comment
Share on other sites

You would probably need more server side scripting to return the contents of a folder. I mean you can just try, but I don't think AJAX is going to return to you a bunch of files in the folder you are in. To create a file manager, you are going to need a sever side scripting language, like PHP, which has native support for reading and writing to directories, in conjunction with AJAX. The server side script will probably end having to do most of the work, giving a response that could be JSON encoded with the list of files so that you can display that to the client.

Link to comment
Share on other sites

You still need to install "the server". It's just that it won't be "Apache and the PHP interpreter", but "the CommonJS implementation".Client side JS simply can't do anything "permanent" that would affect all users of a site, such as writing files on the server.As for the AJAX thing... AJAX is really JavaScript sending HTTP request and receiving the responses instead of your browser. Imagine it like that - when YOU (the user) do HTTP requests by entering a URL into the browser, YOU (the user) are starting the request, the browser executes it, fetches the response, and shows YOU (the user) the result by visualizing the result on screen. With AJAX, JavaScript (the person who wrote it) is starting the request, the browser executes it, fetches the response, and shows it to JavaScript (the person who wrote it), from which point JavaScript (the person who wrote it) can do whatever it wants based on the received data.

Link to comment
Share on other sites

It is certainly possible to write a file manager using AJAX. I suggest you work on the server side first, using a script that just runs when you call it from the URL bar.Job one would be outputting a list of all filenames in a specified directory, not including . and ..Job two would be to reformat the list of names into a useful structure, like JSON.Then maybe think about building a browser document for fetching and displaying the list.

Link to comment
Share on other sites

Forget about CommonJS at least until you understand AJAX better.You can output JSON data from PHP with json_encode().You can read JSON data in JavaScript with JSON.parse() (available as a library for browsers with no native support).(IHMO, server side JavaScript is a very bad idea, especially for newbies, because the client and server start to blend in your mind, when you should instead be separating them to understand what's happening)

Link to comment
Share on other sites

The author of the library - Douglas Crockford - is the one who defined JSON (i.e. created a spec and a reference implmementation)... it has turned into a de facto standard since then (as opposed to a "formal" standard by someone like ECMA, ISO or W3C).It was only this year that it became part of the next formal ECMAScript (i.e. JavaScript) standard, and implemented by IE9.

Link to comment
Share on other sites

Would ya look at that lol. Would this still hold true, "In browsers that provide native JSON support, JSON parsers are also much faster than eval."? I don't see an example of

var Beatles = { "Country" : "England", "YearFormed" : 1959, "Style" : "Rock'n'Roll"}equivalent to:var Beatles = new Object();Beatles.Country = "England";Beatles.YearFormed = 1959;Beatles.Style = "Rock'n'Roll";

in w3schools. Or even

alert(Beatles.Style); //Dot Notationalert(Beatles["Style"]); //Bracket Notation

Should change thread name to something that reflects the content?

Link to comment
Share on other sites

As JSON becomes natively available in all browsers, I'm sure they'll consider giving an example of it, as they have with AJAX.("sure" as in "gut feeling"... I know nothing for real)

Link to comment
Share on other sites

@Post #13:I don't see one on this page http://www.w3schools.com/js/js_objects.asp, which would seem to be the object equivalent of the array example I linked you to in Post #11. There are likewise examples of reg ex literals, so having an example of an object would seem a natural addition.You might repeat that first block of code with a short suggestion in the suggestions forum. You might even mention JSON, since the idea is so closely related. It does seem like something the School should be interested in. And I don't think we need to wait for JSON to be formally recognized, since as I've said, it basically adheres to array/object literal notation. It's simply a way of implementing what already exists.Unless the mods have a direct pipeline, I believe the suggestions forum is the only way the Refnes folks receive.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...