Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:41:42+00:00 2026-06-01T11:41:42+00:00

How can i rewrite all external links with a single onmousedown event? Kinda like

  • 0

How can i rewrite all external links with a single onmousedown event?

Kinda like what facebooks does with it’s link shim

Please check the original Facebook Article about the link shim:
http://on.fb.me/zYAq0N

Requirements:

  • Should work for all subdomains on mainsite.com.

  • Should redirect example.com to
    mainsite.com/link.cgi?u=http://example.com

  • Should only redirect links not belonging to mainsite.com.

Uses:

This is for security of the users of a private web app, and to protect referrers.

Best to do it on the fly for good UI as pointed out in the FB article.

That’s so when users hovers over some link it shows the correct link but when he clicks it, js redirects it to my.site.com/link.cgi=http://link.com

Thanks in advance

SOLVED: https://gist.github.com/2342509

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:41:43+00:00Added an answer on June 1, 2026 at 11:41 am

    For an individual link:

    function $(id){ return document.getElementById(id); }
    $(link).onclick = function(){ this.href = "http://otherlink.com"; }
    
    <a href="http://example.com" id="link">My Sheisty Link</a>
    

    So to get all external links:

    window.onload = function(){
    var links = document.getElementsByTagName("a");
    for(a in links){
      var thisLink = links[a].href.split("/")[2];
      if(thisLink !=== window.location.hostname){
        links[a].href = "http://mainsite.com/link.cgi?u=" + links[a]; }
      // Or you could set onclick like the example above
      // May need to accommodate "www"
    }}
    

    Edit:
    Found this answer and got a better idea.

    document.onclick = function (e) {
     e = e ||  window.event;
     var element = e.target || e.srcElement;
    
    if (element.tagName == 'A') {
     var link = element.href.split("/")[2];
      if(link !=== window.location.hostname){
        links[a].href = "http://mainsite.com/link.cgi?u=" + links[a]; }
        return false; // prevent default action and stop event propagation
      }else{ return true; }
    }};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what does ^ stand for in htaccess rewrite codes. Where can I find all
How can I rewrite this code to check for all characters including the swedish
In javascript, you can rewrite a function, like so: function foo() { setTimeout(function() {
How can I rewrite this code to coffeescript: $('.delete_action').click(function(event) { $.get('/delete_action?name=' + event.target.name +
Is there a way that I can rewrite a folder so that all the
I'm trying to rewrite all request paths to my initiation file. Like so: RewriteRule
how I can rewrite this: for (int i = 0; i < numberOfSpaces; i++)
How can I rewrite he following code using C#3.0 automatic properties? private int _myValue;
How can I rewrite Apple's GLCameraRipple example so that it doesn't require iOS 5.0?
How can I rewrite our commit history to ensure certain keywords never appear? Background:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.