Jump to content

change target attribute of link

Go to solution Solved by crevice,

Recommended Posts

Site URL: https://www.johncervantes.co.uk

I've made my Instagram social link (found in the site header) actually point to a gallery page on my own website. I'd like for this link to open in the same tab. I'm just a hobbyist coder and have come up with in code injection. But it's not working - is it anything to do with when the code will run?

<script>
function changeTarget() {
const instaLink = document.querySelectorAll('[href="https://www.johncervantes.co.uk/instagram"]');
instaLink.setAttribute("target", "_self");
}
  changeTarget();
</script>

 

Edited by crevice
code is in code injection
Link to comment
Quote

is it anything to do with when the code will run?

I believe that is the issue. You can try wrapping it in the following manner.

window.addEventListener ( 'DOMContentLoaded', ( ) => {

  const selector = '[href="https://www.johncervantes.co.uk/instagram"]';
  
  const instaLink = document.querySelectorAll ( selector );
  
  instaLink.setAttribute ( 'target', '_self' );
  
  } );

Let us know how it goes.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment

Thanks very much for this. Unfortunately it's still not working. The error given in console is "Uncaught TypeError: instaLink.setAttribute is not a function"

So presumably it still can't find the element... Then I realised that querySelectorAll returns a NodeList of all the elements it finds? https://developer.mozilla.org/en-US/docs/Web/API/NodeList

So I'll need to iterate over all of them. I'll think about this and try tomorrow.

Link to comment
9 hours ago, crevice said:

Then I realised that querySelectorAll returns a NodeList of all the elements it finds? https://developer.mozilla.org/en-US/docs/Web/API/NodeList

Yep that could be an issue. Iterate, or take the zeroth node.  Or use querySelector if you're sure there is only one element.

My apologies. I was a bit rushed for time so I didn't go deep into your issue. If I have some more time I'll try again if you don't solve it before I have a chance at a second run.

Usually though the DOMContentLoaded is a good idea for most code. 😉

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • Solution
Quote

Usually though the DOMContentLoaded is a good idea for most code

Yes thanks for that, I've managed to get it working:

window.addEventListener('DOMContentLoaded', () => {

    changeTarget = (link) => {
        link.setAttribute('target', '_self');
    }

    const selector = '[href="https://www.johncervantes.co.uk/instagram"]';
    const instaLink = document.querySelectorAll(selector);
    instaLink.forEach(changeTarget)

});

And I'll wrap in the DOMContentLoaded if I need to add anything in the future.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.