1

I am developing a Greasemonkey script and this script changes some elements in a table. However, when we click a button in the table an asynchronous postback happens and the table is refreshed.

So, my elements are also refreshed. How do I know a postback has occurred so that I can run my script again to change the new elements in the table? (The page uses Telerik.)

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
sevenkul
  • 966
  • 1
  • 8
  • 14
  • possible duplicate of [How can I detect AJAX node insertion without using DOM mutation events?](http://stackoverflow.com/questions/10664669/how-can-i-detect-ajax-node-insertion-without-using-dom-mutation-events) – Brock Adams Feb 25 '13 at 15:28
  • I wondered if there is a Telerik event that can be set to run a function after ajax completed. Later, I found an event of ASP.NET 4 http://msdn.microsoft.com/en-us/library/bb397523%28v=vs.100%29.aspx – sevenkul Feb 26 '13 at 09:05
  • Yes, you can run functions after AJAX complete. The problem is that the implementation can get messy for a user/GM script and you often need timing delays anyway. On complex sites, filtering out the AJAX that you don't care about must also be done. Bottom line is you don't care about the AJAX, you care about the result. Monitor results, and the mechanism becomes irrelevant (most of the time). – Brock Adams Feb 26 '13 at 10:59

2 Answers2

1

Don't bother trying to check for a postback That's almost always a painful way of doing things. You care about the table content, monitor that.

See:

  1. Run Greasemonkey script on the same page, multiple times?
  2. Fire Greasemonkey script on AJAX request
  3. How can I detect AJAX node insertion without using DOM mutation events?

and several others.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
0

Just as Brock Adams says in his answer, I decided to monitor table and wrote this code:

function repairInputEvents() {
    for (var i = 0; i < document.getElementsByTagName("input").length; i++) {
        //Add event to buttons that cause postback.
        document.getElementsByTagName("input")[i].addEventListener("click", function() {

            //Run the following just before postback.
            document.getElementsByClassName("table")[0].setAttribute("title", "old");
            var checkAjaxInterval = setInterval(function() { checkAjax(); }, 1000);

            function checkAjax() {
                if (document.getElementsByClassName("table")[0].getAttribute("title") != "old") {
                    clearInterval(checkAjaxInterval);
                    repairInputEvents();
                    repairSelectEvents();
                }
            }

        });

    }
}

//First run
repairInputEvents();
repairSelectEvents();
sevenkul
  • 966
  • 1
  • 8
  • 14