How to run javascript on server side of shopify

Hello Guys,

I need to add the rel=“nofollow” attribute to some items on the mega menu and footer of my store. After finding out, it seems that it can only work with javascript. However, the problem is that if javascript runs on client-side, the rel=“nofollow” will not display in the HTML code.

Hence, I want the javascript to run on server-side. Do you guys know how to make the javascript to run on server-side?

I really appreciate if you can help me with this issue.

Hi @lmchinghung

Running script on server is similar to write in liquid.

You can share the logic may be we could help.

Thank you for your reply. Below is the content of the Js file:

document.addEventListener(‘DOMContentLoaded’, function() {

var linksToNofollow = document.querySelectorAll(‘a[href=“/collections/i-love-outlet”],a[href=“/collections/i-love-outlet”],a[href=“/collections/expired-items”],a[href=“/collections/sale-apparel-accessories”]’);

linksToNofollow.forEach(function(link) {
link.setAttribute(‘rel’, ‘nofollow’);
});

});

It will scan for the a[href] and then add rel=“nofollow” to it.

We can write this custom code in your theme to achieve this.

Let me know if interested in hire.