Lets say I have the following code
// JS
$(function(){
$('[data-mc=logout]').click(function(){
if (!confirm(myconfig.msg['asklogout'])) {
return false;
}
});
$('[data-mc=ajax]').click(function(){
var target=$(this).data('target'); //retrieve data-target of the clicked tag
return false;
});
});
which target some HTML elements
// HTML
<a class='menulink' data-mc='logout' data-target='main-content-div' href='page/'>Logout</a>
<a class='menulink' data-mc='ajax' data-target='main-content-div' href='page/'>Start page</a>
Am I right when I say that this code needs to be called on every full page load?
a: If so, what if one of the elements doesn't exists on the chosen page?
b: To avoid errors an "if exist" check is necessary, right?
c: jQuery will do that prior adding the handler?What if some content gets requested using Ajax and inserted into the DOM. Then this code needs to be called again, right?
Is this best practice or should the code be split and called per page load, where only existing element's event handlers gets attached?