jQuery in Action

4.1
All Stack Overflow 20
This Year Stack Overflow 4
This Month Stack Overflow 4

jQuery in Action

4.1

Review Date:

Comments

by Russ Cam   2017-08-20

Unobtrusive JavaScript is the main driver for separating JS code from markup

  • Separation of functionality (the "behavior layer") from a Web page's
    structure/content and presentation
  • Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
  • Progressive enhancement to support user agents that may not support
    advanced JavaScript functionality

With the code in document.ready, it will not execute until the DOM has loaded and before the page contents are loaded

From Learning jQuery

With $(document).ready(), you can get your events to load or fire or whatever you want them to do before the window loads.

You may want to take a look at jQuery in Action, I'd highly recommend it. You can sample Chapter 1 and Chapter 5 on the book's homepage. I think doing so may provide further insight into why separation can work well.

Finally, have a look at this question which has answers that detail how you would find the event listeners on a DOM node.

EDIT:

Some thoughts which may persuade you why unobtrusive JavaScript can be a good idea.

Imagine that you have a function bound as an event handler for the same event raised on each of a number of elements -

  • Would it be easier to find out which elements call that function to handle an event when the declaration is inline in each element, or the declaration is in one place, outside of the markup?

  • What if you want to add to the elements that call that function when the event is raised on each of them? Would it be easier/better to add the event handler inline to each element or to change the code in one place?

by anonymous   2017-08-20

I'm currently reading jQuery In Action and I find it easy to read with great examples.

There are other questions on SO that provide many other resources.
https://stackoverflow.com/questions/881002/jquery-resources
jQuery & ASP.Net Resources & Gotchas

by anonymous   2017-08-20

At the jQuery's homepage jQuery.com you can find several tutorials.

Further on there are books written about jQuery. The one I can recommend is the jQuery in Action. It's a pleasant read.

Here's a showcase of sites which use jQuery:
http://usejquery.com

Here are more topics about the subject:
Where can I find a tutorial to get started learning jQuery?
Where can I learn jQuery? Is it worth it?

by anonymous   2017-08-20

Your actual problem is that you don't know how to process the JSON string in the client side. I strongly suggest to use jQuery for this since this simplifies DOM manipulation a lot. I suggest to go through their tutorials or go get a decent book on the subject.

For the jQuery + JSON + Servlet + HTML table mix, I've posted similar answers before here and here with code examples how to populate a table with help of Google Gson and a Servlet, you may find it useful. I'll copypaste from one of them.

Here are the Servlet and the Javabean:

public class JsonServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Data> list = dataDAO.list();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(new Gson().toJson(list));
    }
}

public class Data {
    private Long id;
    private String name;
    private Integer value;
    // Add/generate getters/setters.
}

The JsonServlet (you may name it whatever you want, this is just a basic example) should be mapped in web.xml on a known url-pattern, let's use /json in this example. The class Data just represents one row of your HTML table (and the database table).

Now, here's how you can load a table with help of jQuery.getJSON:

$.getJSON("http://example.com/json", function(list) {
    var table = $('#tableid');
    $.each(list, function(index, data) {
        $('<tr>').appendTo(table)
            .append($('<td>').text(data.id))
            .append($('<td>').text(data.name))
            .append($('<td>').text(data.value));
    });
});

The tableid of course denotes the idof the HTML <table> element in question.

<table id="tableId"></table>

That should be it. After all it's fairly simple, believe me. Good luck.

by rogerpence   2017-08-20

Rick Strahl and Matt Berseth's blogs both tipped me into jQuery and man am I glad they did. jQuery completely changes a) your client programming perspective, b) the grief it causes it you, and c) how much fun it can be!

http://www.west-wind.com/weblog/

http://mattberseth.com/

I used the book jQuery in Action http://www.amazon.com/jQuery-Action-Bear-Bibeault/dp/1933988355/ref=sr_1_1?ie=UTF8&s=books&qid=1219716122&sr=1-1 (I bought it used at Amazon for about $22). It has been a big help into bootstrapping me into jQuery. The documentation at jquery.com are also very helpful.

A place where jQuery falls a little flat is with its UI components. Those don't seem to be quite ready for primetime just yet.

It could be that Prototype or MooTools or ExtJS are as good as jQuery. But for me, jQuery seems to have a little more momentum behind it right now and that counts for something for me.

Check jQuery out. It is very cool!

by Don   2017-08-20

Chapter 7 of JQuery in Action is "Extending JQuery with custom Plugins" and should provide you with the information you need.

by Eric Duncan   2017-08-20

Stay away from the UpdatePanel concept all together.

ASP.NET MVC includes jQuery, which is fully supported by Microsoft now. You will want to create partial views (RenderPartial) that make calls back to a method on a controller, that returns JSON.

Then, use jQuery to wire up the control and partial views.

jQuery is an extremely powerful javascript library. I highly recommend the book jQuery in Action as a reference when diving into the ASP.NET MVC /Scripts/jquery-x.x.x.js files. :)