A short script was created to use ajax to load pages. Only links with the class ajax-pls
should be selected.
Once an eventlistener
is added, the class is removed so that the included HTML can be parsed each time.... right?
(function() {
function addEvent(element, evnt, funct){
if (element.attachEvent)
return element.attachEvent('on'+evnt, funct);
else
return element.addEventListener(evnt, funct, false);
}
var link_click = function (e) {
e.preventDefault();
if (this.getAttribute("href") == 'test1.html') {
var content = document.getElementById('content');
content.innerHTML = "<a href='test3.html' class='ajax-pls'>Test3</a>";
register_listeners();
} else {
alert(this);
}
};
function register_listeners() {
var atags = document.querySelectorAll('a.ajax-pls');
for (i = 0; i < atags.length; i++) {
addEvent(atags[i], 'click', link_click);
atags[i].classList.remove("ajax-pls");
}
}
register_listeners();
})();
This code snippet is just for testing purposes, but is it necessary to remove the class or could I simply call register_listeners()
after each inclusion?