-3

I am totally new to javascript.

I have a site mysite.com, with tonnes of External links and I am trying to track every external link and append it as parameter to an external domain like

myanothersite.com/redirect.html?url=http://externallink.com`

using javascript.

myanothersite.com/redirect.html is a page which it redirects to passed url parameter after 5 seconds and i have created this page successfully.

In a nutshell, when a visitor clicks on external link, I don't want him to visit the external site directly. Instead I want to do this,

mysite.com 
--> Visitor clicks on http://externallink.com 
--> myanothersite.com/redirect.html?url=http://externallink.com 
--> http://externallink.com

so, here I am trying write a js code that automatically detects external link and append it as a query parameter to

myanothersite.com/redirect.html?url=

mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

-1

there is a package on npm which may help you

qs

In addition, the. window object is a global object which can handle redirects, open a new window, or reload. So you can go through the documentations and red that one as well.

If you are looking for a simpler solution, you can try this

grab the link from the window object like this and store in a variable

var location = window.location.href

Then split it around the 'url=' like this

var splitArray = location.split('url=');
console.log(splitArray[1]);

You should see your url in the console. use it wherever you want