Home

Issue: 2912

How to replace attributes in links?

[issue link]

Hi. I have small script in my default layout

mounted () {
    let links = document.querySelectorAll('a[href]:not([href^=mailto]):not([class^=addfollow])')
      console.log(links)
    for (let i = 0; i < links.length; i++) {
      if (links[i].hostname != window.location.hostname) {
        links[i].target = '_blank'
        links[i].rel = 'noopener nofollow noreferrer'
      }
    }
  }

This adds target="_blank" to all links on the page.
It works fine, but when I hit 404 (I have 404 layout for that) and after returning back - scripts doesn’t work, all links remains unchanged.

Is there’s the way to fix this? Where shoould I put such scripts using Nuxt? Maybe placing it in default.vue layout is not good idea?

This question is available on Nuxt.js community (#c2528)