jQuery

Jquery External Link Disclaimer Alert (a.k.a. JELDA!?)

We ran into a situation where a website needed to have a disclaimer for external links.  That is, if they're on the site and there's a link for Apple.com, an alert would popup with a disclaimer saying something like "Hey there Senor bitchy-pants, this here link is offsite and it could be filled with any crazy sort of content imaginable, like a monkey washing a dog or really hairy guys dressed as Wonder Woman." and then let them hit OK or cancel.
 
This proved to be something that no one else was able to do, or at least they didn't post about it.
 
Thanks to my cow-orker, Pfrilling, we came up with a simple and elegant jquery-based solution.
 
Here's the jquery code:
 
 
if(Drupal.jsEnabled) {
  $(document).ready(function() {
 
  $('a').filter(function() {
   return this.hostname && this.hostname !== location.hostname;
 })
 .click(function () { 
 var x=window.confirm('You are about to proceed to an offsite link.  Auglaize County has no control over the content of this site.  Click OK to proceed.');
var val = false;
if (x)
val = true;
else
val = false;
return val;
 
        });
  });
}
 

Now let's break it down.
  • First, the script targets all links and checks to see the link is external or not.
     
  • Then it says when you click a link that is external, pop up a dialog box.
     
  • If they click 'cancel', do nothing at all.
     
  • If they click "OK", proceed with the link they clicked.
 
Kapeesh, home-doggies?
 
We used it inside a Drupal site, but it can work anywhere.  Very neat that basic javascript can be used inside of Jquery!
 
Here's hoping it's useful to someone out there.

 

Syndicate content