Skip to content

Instantly share code, notes, and snippets.

@anag0
Created Oct 5, 2021
Embed
What would you like to do?
ASP: Change links target to _blank if external native javascript without jQuery
functions.php
<?php
// ---- !!! ONLY COPY PASTE CODE BELOW THIS LINE to the functions.php in your theme directory !!! ----
add_action('wp_footer', 'asp_add_link_check_external', 10);
function asp_add_link_check_external() {
?>
<script>
(function($){
function link_is_external(link_element) {
return (link_element.host !== window.location.host);
}
$(".asp_main_container").on("asp_results_show", function(event, id, instance) {
$('.asp_r a.asp_res_url').forEach(function(el){
if ( link_is_external( el ) ) {
$(el).attr('target', '_blank');
}
});
});
})(WPD.dom);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment