4
On a platform of forums there is a pagination system, and I would like to make this pagination via Ajax, for this, I built the following function:
//<![CDATA[
var Pagination = (function(w, d) {
var pagination = {
init: function() {
this.run();
},
run: function() {
w.addEventListener('DOMContentLoaded', function() {
var links = d.querySelectorAll('p.paging a[href^="/t"]');
for (var i = 0; i < links.length; i++) {
links[i].addEventListener('click', function(event) {
event.preventDefault();
var link = this.getAttribute('href'),
xhr = new XMLHttpRequest();
xhr.onload = function() {
d.querySelector('.main').innerHTML = this.responseXML.querySelector('.main').innerHTML;
};
xhr.open('GET', link);
xhr.responseType = 'document';
xhr.send();
});
}
});
}
};
pagination.init();
}(window, document));
//]]>
It works perfect until the first click. But if I click on more than one link the function does not work at all and the page is loaded normally.
I’m pretty sure the mistake is in my loop for()
, i haven’t found a better way to make the Ajax function for each time I click on a link from that pagination.
If there’s any better way, I’m glad to know.
@Edit:
The structure of my HTML is like this:
<div class="main">
<p class="paging">
<a href="/topic/1">1</a>
<a href="/topic/2">2</a>
<a href="/topic/3">3</a>
</p>
<div class="topic">
<div class="content">Conteúdo aqui.</div>
</div>
</div>
Are you clicking on links that already existed on the page, or on links that were added after the first ajax call? P.S. at first glance, doesn’t seem to have anything wrong in your loop for.
– mgibsonbr
@mgibsonbr They already exist on the page, they are created via PHP (which we do not have access to), usually by clicking it will redirect to the next page.. I have tried several ways but still have not achieved a satisfactory result. It works on the first click but stops working from the following clicks.. As if I was using Document.querySelector()
– waghcwb
The console points out some error?
– Bruno Augusto
@Brunoaugusto, no, it shows no error. Neither Warning, nothing..
– waghcwb
@waghcwb the links from
.paging a
are inside the.main
?– Sergio
Have you tried isolating the logic of this "class" to see if it works?
– Bruno Augusto
@Sergio, that was exactly it, buddy. I switched the selector to one that didn’t include the pagination inside it, and it worked perfectly. I appreciate the attention of all of you friends, you are awesome!
– waghcwb
@waghcwb great! If you want to delete the question; or put HTML for me, or you can answer and maybe be useful for others.
– Sergio
@Sergio, made friend.. Best leave it to the content available to everyone, maybe someday someone will face the same problem.
– waghcwb
@waghcwb, I got a question when you put HTML. You’re rewriting HTML inside the main every time you make an AJAX request, right? This will cause you to lose the Vent handlers... so or run
pagination.init();
every success of AJAX or replaces only the content of.topic
. What are you doing?– Sergio