hytromo
hytromo

Reputation: 1531

Not only wait for page to load but for a content of it to be updated via an ajax call

I am using jQuery and ajax to get a remote webpage. If you load this webpage into your browser, it loads fine, but after ~100ms another element is being shown that represents a counter. This counter is inserted at a specific place and has a specific class. This is the HTML of this place:

<span class="unreadcount">1</span>

I am trying to make a plugin for chrome and it needs this value. The thing is that this value is not available after the page is loaded, but I have to wait for a request from the page to the server (for the unreadcount number) to finish, before processing the page's HTML for getting the value I want.

Is there any way I can do this with jQuery? I am using the following code to get the HTML page:

$.ajax({
           async: false,
           cache: false,
           type: 'GET',
           dataType: 'html',
           url: 'https://somesite.com/index.php',
           success: function(data1) {
               var descNode = document.createElement("div");
               descNode.innerHTML = data1;
               var unreadcount = descNode.getElementsByClassName("unreadcount");
            /*process unreadcount etc*/
         },
         error: function () {
            // something went wrong with the request
            error_occurred();
            return;
         }
      });

Unfortunately, each time, unreadcount.length is 0 and I noticed that the counter wasn't loading along with the page, but shortly after.

How can I wait for the counter to load before processing the page?

Upvotes: 0

Views: 135

Answers (1)

Alexander
Alexander

Reputation: 23537

You can't.

You are getting the webpage as a string. Hence, no JavaScript will be executed.

Your best bet is to try to replicate the request which fetchs the unread mail count. That is, if you are sure it is done via AJAX.

Although, probably using an <iframe> may be of help. And, in your favor, you only need an <iframe> solution that works only in Chrome, not a cross-browser <iframe> solution which is prone to fail.

Upvotes: 1

Related Questions