views:

180

answers:

2

Hi,

i am receiving a piece of plain html from an ajax request.

<h1>Title</h1>
<div>
  content
</div>

This is the most simple form. Every piece contains a <h1> tag for a title, and a <div> tag containing the content. I have a nicely formatted container in the html page which needs to populate with the returned html snippet.

This is the container:

<div id="container">
  <div class="header">
  </div>
  <div class="content">
  </div>
</div>

I use the following javascript function to parse the html and place it in the container.

function loadContent(id, data) {
    var container = $('#'+id);
    var title = '';
    var content = '';

    $(data).filter('h1:first').each(function() {
        title = $(this).html();
        content = $(this).next().html();
    });

    $('div.header',container).html(title);
    $('div.content', container).html(content);
}

Everything seems to be working 'allright', subsequent ajax requests that have different html contents load pretty quickly. But when I click a link that invokes a full page refresh, it hangs for about 3 or 4 seconds before loading clicked hyperlink. This makes me think it is a javascript issue, maybe where some content stays in memory? Can somebody see where this might become inefficient?

+1  A: 

I would use a debugger to find the source of the delay. Something like FireFox w/FireBug to verify that the link that triggers the full page refresh is actually calling this code and not getting hung up elsewhere. And FireFox w/TamperData to watch the requests that are going out and coming back to see if the delay is caused by waiting on an external response.

:(

ongle
i use firebug, everthing loads fine, only with delays... it must be the javascript
Ropstah
If you are just clicking a link (<A>) why would this javascript be invoked? Can you comment out part or all of the javascript to test the theory?
ongle
+2  A: 

I'm not sure if I understand the intent here. If you're only assigning one title/content pair, why the .each()?

Also, keep in mind that Firebug often causes significant performance issues itself. Be sure to test with it disabled before assuming your code is the problem.

Finally, if that's not it, use Firebug's profiler to determine exactly which part of your code is running slowly.

Update, based on comment. Try this instead:

function loadContent(id, data) {
  var container = document.getElementById(id);

  var title = '';
  var content = '';

  $data = $(data);
  $title = $data.filter('h1:first');

  title = $title.html();
  content = $title.next().html();

  $('div.header', container).html(title);
  $('div.content', container).html(content);
}
Dave Ward
I'd also use `find('h1')` and `find('div')` if there were only one of them, and the whole thing could become much smaller: function loadContent(id, data) { var container = $('#'+id); $('div.header',container).html($(data).find('h1').html()); $('div.content', container).html($(data).find('div').html()); }
aditya
@aditya: the find function does not find the tags in return output. It is just plain text, maybe that's why it doesn't work?
Ropstah
@Dave. The problem persists when not using each. I've just tested on another pc without firebug, same problem. Also, when I profile the code, the bottleneck seems to be at the F() function used by jQuery. It's called a lot of times, but not only by the piece of code i'm providing. Problem is to find out where the issue really is.... The website is quite big and runs smoothly, only this piece of ajax/js functionality seems to mess up the performance....
Ropstah
See the update. That should be faster in a couple of aspects. Without seeing the real page in question, it's hard to know what specifically is slowing you down, but maybe this will help.
Dave Ward
@Dave: I ran some tests, and `find()` won't work — but for different reasons. `find()` looks for children, and only children. My bad.
aditya