Masking URL's with JavaScript

Sometimes you build a site and think, I really don't want people seeing who I'm linking up to.  There could be a variety of reasons of why you would want to.  This isn't that post.  This post simply lays out one method to mask URL's in your hyperlinks.

To mask your links, simply add the following JavaScript to your page, somewhere close to your opening <body> tag, but after it:

<script type="text/javascript">
<!--
    function GetResource(url){window.location = url;}
    function GetNewResource(url){window.open(url);}
//-->
</script>

The two functions perform the redirect, each a bit differently.  The GetResource(Url) routine accepts the Url and changes the current widow location to the provided Url.  The GetNewResource opens a new browser window and navigates to the provided Url.

With that in place, we now need to modify all the links on the page that we don't want the user to readily know who we are linking to.  This is accomplished by making the statusbar in the browser display a # symbol.  Our next step is to modify the links on the page.

From this:

<a href="http://www.MyAffiliateLink.com/blah/blah.bla">Not a masked link</a>

To this:

<a href="#" onclick="javascript:GetResource('http://www.MyAffiliateLink.com/blah.bla')">
A Masked Link</a>

How you modify the links on your pages is another post entirely, but if you just have static Html files and not many files, you can parse though this by hand.  If you're your on the programming side you can parse the Html prior to it being written to the client, modifying the links as you go.

permalink Permalink or Trackback Comment Comments (0) Cat Web Development
Technorati: No reaction yet!
Tags: , ,
Actions: E-mail

Was this helpful?

If you liked this or found it helpful, please digg it, stumble it, buzz it, whatever it, to say thank you.





Add to Technorati Favorites

 
 If you would like to receive these posts as they happen, you can subscribe to my feed or receive my posts in your email.

Related Posts

Add comment



(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading



Check it out mango: Any links must be entered as http://www.somewhere.com with nothing touching it. Anything else will be mangled. This is to help combat spam and to also ensure the masses know of this little tidbit before they click Save comment below. :) I have this down to remind me to do something with it, but I take things slow and easy on the old horse.

Keeps her regular don't ya know, and I wouldn't want to disturb that.



CSS Template by RamblingSoul | Illinois Wine. Adapted to BlogEngine by Wayne John
EatonWeb Blog Directory  Blog Directory Blogger Forum: About Blogging for Bloggers DaniWeb - IT Professionals' Lounge Community