Oliver Nassar

Remote URL Opener (JavaScript Class)

June 01, 2022

The idea for this JavaScript class comes from my experience with Iconduck. Specifically, that site has a ton of outbound links to icon and illustration creators.

One thing I wanted to do is have all 3rd-party outbound links open in a new window, and include some URL params so that the creator of that site might be able to see where traffic was coming from.

In essense, I wanted a simple class to convert:

<a href="https://example.com/">Iconduck</a>

into:

<a href="https://example.com/?ref=iconduck.com" target="_blank">Iconduck</a>

By building this into a JavaScript class, I'm able to automate this process and have the code take into consideration the current host.

When it finds a link that is pointing to a 3rd-party host, it'll jump in and make the needed changes.

It's been open sourced, and is available on GitHub.


Sample Links (try clicking)


Implementation Code

RemoteURLOpener.init();
RemoteURLOpener.setRef('onassar.github.io');

Extras

When it finds a link that is pointing to a 3rd-party host, it'll jump in and make the needed changes.

Enjoy :)


References