I've come to the point where I need Ajax on my page, but it's just for one small part - to see if a username typed in is in the database. As explained here Ajax can be done with JavaScript alone. What are the pros/cons of doing it this way? I'm leaning towards this because I don't want a large library and think it's unnecessarily complicated when everything else is already JavaScript alone.
-
it's the same, nor worst not better, 100% the same – mikakun Feb 07 '13 at 09:04
-
jQuery makes ajax calls shorter in your code as well as binding events and lot of other stuff. If you have to write the if(IE&&version ... every time your code would be very lengthy indeed. You might find yourself using jQuery when you actually use a lot of it's functions but if you only plan to use it for one AJAX call then better not use it at all. – HMR Feb 07 '13 at 09:07
-
1If you load jQuery from a CDN such as Google's then there's a good chance visitors to your page will already have it cached from visiting other web pages. Though the compressed version of jQuery really isn't that large, only 90.5 KB. – Anthony Grist Feb 07 '13 at 09:17
-
1http://microjs.com/#ajax lists many small libraries to perform AJAX requests. – Alessandro Vendruscolo Feb 07 '13 at 09:26
-
My advice is to stop using things like the jQuery library and start learning native _JavaScript_. It's easier than you think (yes, even AJAX). – delete me Nov 06 '16 at 12:39
-
@Jase why don't you at least explain yourself? – Celeritas Nov 06 '16 at 19:44
-
@Celeritas because the answer is obvious, and can be found with a single Google search. This question doesn't even meet the requirements for posting a question on this site. And you want me to explain myself? Explain yourself. – delete me Nov 13 '16 at 12:50
-
@Jase so you think if I search "why jquery and javascript frameworks are bad" it will give the same reasons you are thinking of? – Celeritas Nov 13 '16 at 21:33
-
@Celeritas I'm sorry. I was in a bad mood the other night. If you haven't already, you might want to have a look at this link [Advantages of Using Pure JavaScript over jQuery](https://softwareengineering.stackexchange.com/questions/166273/advantages-of-using-pure-javascript-over-jquery) - there are quite a few good pro's/con's for both. I guess it just depends on your use-case, but I would argue that maintainability would be much easier and thus more efficient if you were to use pure JavaScript. On the other hand, libraries also reduce development time and have the benefit of (1/2) – delete me Nov 15 '16 at 12:12
-
(2/2) ensuring cross-browser compatibility for you in many cases. – delete me Nov 15 '16 at 12:13
6 Answers
If you don't need to support older version of IE, like IE6, then it's quite simple, you don't need any factory function, just a plain:
var http = new XMLHttpRequest();
For all browsers. Plus, in recent browsers (I believe also in IE8), you can simplify more using onload
events instead of onreadystate
:
var http = new XMLHttpRequest();
http.open("GET", "somepage.html", true);
http.onload = function () {
alert("Request complete: " + http.responseText);
}
http.send();
That is quite similar to the success
handler of jQuery.
For further details, see: Using XMLHttpRequest
However, jQuery now has the ajax calls threat as promises, that makes some scenario (like waiting for multiple ajax calls to finish before run some code) much easier to develop.
It's not that bad... just not as small: Small Ajax JavaScript library as explained there, you could also just get one of those small libraries that handle ajax alone tho.
Edit: As MCL pointed out below in his comment, there is also this snippet: https://gist.github.com/mythz/1334560
It seems to implement the jQuery syntax but only for ajax calls, if you're used to it, this might be a better choice!

- 1
- 1

- 2,249
- 2
- 19
- 39
-
1I found an interesting [ajax standalone library](https://gist.github.com/mythz/1334560). I didn't test it, but it seems like `$.ajax()`calls can be made with the original jQuery syntax. This would make them easily interchangeable. – MCL Feb 07 '13 at 09:13
-
for people who are used to the jQuery syntax that'd probably be a good lib, if you don't mind I'll add it to the answer and point to the comment section. – Gonçalo Vieira Feb 07 '13 at 09:17
-
Of course. I believe with this library as a basis, it is not very hard to add utility wrappers like `$.post()`, `$.getJSON()` etc. – MCL Feb 07 '13 at 09:19
It's not bad at all, jQuery just provides a shortcut to do it, while writing it all manually gets a little bigger. That's pretty much it.
If you just want an "ajax helper", try using this snippet from quirksmode.: http://www.quirksmode.org/js/xmlhttp.html

- 36,317
- 49
- 147
- 195
jQuery is just a framework which allows us to do AJAX calls more efficiently. Lesser code. It does not matter whether you use jQuery or not. That's totally up to you to decide. If jQuery is heavy-weight just use a library where only it provides AJAX functionality only. Also you can pure JS to make AJAX calls.

- 44,706
- 42
- 157
- 243
I think the biggest pro is that jQuery solves the cross-browser way of handling AJAX calls.

- 222
- 1
- 5
Nothing difference other than loading a Jquery.min.js of 80kb on your page...

- 120,458
- 37
- 198
- 307