• Welcome to WebsiteBaker Community Forum. Please log in or sign up.
Welcome, Guest
News
Support WebsiteBaker
Your donations will help to:
PayPal
  • Pay for our dedicated server
  • Pay for domain registration
  • and much more!
You can donate by clicking on the button below.

Automatic external links and rel="noopener"

Started by Hans, May 14, 2017, 08:54:47 PM  ( Read 9,454 times )

Hans

Hello,
unless my client doesn't want that "feature" I always place this code in my templates to force external links to open in a new tab.

<
script type="text/javascript">
$(document).ready(function () {
$("a").filter(function () {
return this.hostname && this.hostname !== location.hostname;
}).addClass('external').attr("target", "_blank");
});
</
script>


Now I have read that there might be safety issues and negative impact on quick rendering of a page.

To avoid those it's advisable to add rel="noopener" to the link.

I don't know however how to change the code so that rel="noopener" is part of the resulting link.

Hope someone can help me, I'm no coder.

    Hans - Nijmegen - The Netherlands

    Ruud

    The jQuery function attr() also allows objects/arrays to be processed.

    You can change the part:
    .addClass('external').attr("target", "_blank");

    into:
    .addClass('external').attr({
       
    target : "_blank" ,
       
    rel : "noopener"
    });

      Hans

      Hans - Nijmegen - The Netherlands


      \n\n