Updating the href
doesn't work, because the callback function operates asynchronously.
Instead, assign to window.location
to redirect to the new URL instead of performing the default action of the link.
jQuery('a[href^="http"]').not('[href*="' + window.location.host + '"]').on('click', function(e) {
e.preventDefault(); // don't follow the link automatically
let url = jQuery(this).attr("href");
jQuery.ajax({
data: {
action: 'the_ajax_hook',
source: jQuery.cookie('source'),
destination: url,
},
type: 'post',
url: ajax_object.ajaxurl,
success: function(data) {
process(url, data);
}
});
});
function process(url, data) {
let target = url.replace('$id', data);
window.location = target;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…