• Hi,

    Someone told me I can use the javascript below to convert all external links to open in new tab.

    jQuery(document.links) .filter(function() { return this.hostname != window.location.hostname; }) .attr('target', '_blank'); 
    
    Read more: https://html.com/attributes/a-target/#ixzz5dpkM3QDM

    Where do I put the code? In the header part? In the footer?

    I have a plugin that allows me to put code in those parts (and in before/after post)

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Thread Starter Jack Sarlo

    (@redhalls)

    Someone suggested it’s a good idea to limit the number of plugins we use.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    “someone suggested”, yet you’re looking for a plugin to let you insert jQuery. ??

    Go with the easy solution.

    Thread Starter Jack Sarlo

    (@redhalls)

    I thought I could just put the javascript in the header, that would be easy.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Edit your theme’s header.php but be advised that any changes you make to the theme will be lost when/if it’s updated, so you’re best advised to use a child theme or make your own plugin to add it to the header or use one of the plugins that lets you add code to the header. The example you showed above lacks the necessary script tags to be inserted.

    Thread Starter Jack Sarlo

    (@redhalls)

    ok, I can put the following between the <head> </head> tags.

    <script>jQuery(document.links) .filter(function() { return this.hostname != window.location.hostname; }) .attr('target', '_blank');</script>

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’m not sure if the header is the right place for that because it may execute before the page is entirely loaded. It might work better in the footer.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Adding target_”blank” to all links (with javascript)’ is closed to new replies.