With jQuery 1.7+:
Code:
$(document).on('click', '*[data-autoXHRinto]', function(e){
var autoXHRinto = this.getAttribute('data-autoXHRinto');
// here you can parse the data-autoXHRinto with regExp to determine what to do
// do that and or return false and/or do an e.preventDefault() on the click
}
That will listen for clicks on the document. If the target is or bubbles to an element with a data-autoXHRinto attribute, the function will be performed upon that element. I would suggest using the id (and/or other selector) of the recipient element to find the into element rather than using the data-autoXHRinto attribute in an ambiguous way (though that could still be workable I think), and making the data-autoXHRinto a little more complex. Perhaps a comma delimited string that the function could turn into an array and perhaps then into an object:
HTML Code:
<a href="somepage.htm" data-autoXHRinto="href:, content2.htm, id:, someid">Wahtever</a>
<div id="someid"></div>
Then something like so (untested):
Code:
$(document).on('click', '*[data-autoXHRinto]', function(e){
var autoXHRinto = this.getAttribute('data-autoXHRinto').split(/, ?/),
autoXHRintoObj = {}, i = autoXHRinto.length - 2;
while(autoXHRinto[i]{
autoXHRintoObj[autoXHRinto[i].replace(/:$/, '')] = autoXHRinto[i + 1];
i -= 2;
}
if(!autoXHRintoObj.href && this.href){
autoXHRintoObj.href = this.href;
}
if(autoXHRintoObj.href && autoXHRintoObj.id){
$('#' + autoXHRintoObj.id).load(autoXHRintoObj.href);
e.preventDefault();
} else if (autoXHRintoObj.href){
$(this).load(autoXHRintoObj.href);
e.preventDefault();
}
}
Bookmarks