3 ms·
Your jQuery: $(document).ready(function() { $.ajax({ url: "test.html", context: document.body }).done(function() { $(this).addClass
by grannyg00se 14y ago
Your jQuery:
$(document).ready(function() {
$.ajax({
url: "test.html",
context: document.body
}).done(function() {
$(this).addClass("done");
})
});
Without jQuery:
var oReq = new XMLHttpRequest();
oReq.onreadystatechange = function() {if (this.readyState === 4) {
window.document.body.className += " done";
}};
oReq.open("GET", "test.html", true);
oReq.send();
Screenshots (blue indicates successful ajax):
without jQuery: http://browsershots.org/http://evening-harbor-8654.herokuapp.com/testNoJQuery.html http://browsershots.org/http://evening-harbor-8654.herokuapp...
with jQuery: http://browsershots.org/http://evening-harbor-8654.herokuapp.com/testJQuery.html http://browsershots.org/http://evening-harbor-8654.herokuapp...
- neya 14y agoYour code is fine for this use case. But I'd have to bite my nails hoping it would work on browser 'X' for other use cases, which is simply not worth the headache. Cross browser compatibility is a real pain, you know? For me JQuery does it very well. And good luck using your new ajax function :)
- Offler 14y agoDrop support for old IE browsers, why are people still trying to fight to support them. I agree with the blog post, one day people will finally drop support for non-HTML5 browsers (IE8 and below) and they will see how easy it is to write clear, short native code that works fine in all browsers. It will take time though as many people aren't even familiar with the native APIs.
- bjxrn 14y agoYou want to know why people worry about older versions of IE? Because a large enough chunk of their visitors still use those browsers. It's that simple.
- WA 14y ago$.get('test.html', function(r) { window.document.body.className += " done"; }); But: This is not about short code. It's about readability. What's readyState 4? Why "open" a request before sending it?
- shuzchen 14y agoIt gets even messier when you find out one day you have to handle failures, or serialize data to send to that endpoint, or get notified on network events, or cancel an ongoing request, or etc etc etc.
- grannyg00se 14y agoMy code is a response to neya's comment. It's not about short code or readability. It's about demonstrating that neya's example has completely unreasonable non-jQuery code given the jQuery code it is being compared against.