Popup for External Links (jquery)

Travis Black's picture

I have been asked by multiple clients lately to provide a feature that will cause ANY link to an external site, alert the user that they are leaving the site before they go. At first I was unsure how to do this, but jQuery made it beautiful, as it does most things.

Step 1. Download jQuery
Step 2. Download jQuery UI
Step 3. create a file with the following code as its contents.

$(document).ready(function() {
 
  // Creating custom :external selector
  $.expr[':'].external = function(obj){
      return !obj.href.match(/^mailto\:/)
              && (obj.hostname != location.hostname);
  };
 
  // Add 'external' CSS class to all external links
  $('a:external').addClass('external');
 
  $('.external').click(function() {
    var link = $(this).attr('href');
 
    $('<div>You are currently leaving our site, and headed towards: <br /> '+(link)+' <br /> are you sure you want to proceed?</div>').dialog({
      title: "External Link",
      modal : true,
      overlay: {
        backgroundColor: '#000',
        opacity: 0.5
      },
      buttons: {
        'Okay': function() {
          $(this).dialog('close').remove();
          window.open(link);
        },
        'Cancel': function() {
          $(this).dialog('close').remove();
          return false;
        }
      }
    });
 
    return false;
  });
});

Step 4. Include those three files on your site. Remember jQuery first, then UI, then the script pasted above.

Step 5. If you want to use the theme that you downloaded bundled with jQuery UI, you will need to include the .css file from that theme. When you download UI, you get a CSS folder, inside that is a theme folder, and inside that is your css file.

This is a screenshot of what it looks like with the start theme:

Comments

Ben Buckman's picture
Member since:
20 July 2009
Last activity:
23 weeks 6 days

Sweet! This would be a great addon to Drupal's External Links module, which wraps the logic of determining external links in an admin UI.

I need to start using jQuery UI more often...modal boxes are so much better than confirm()!

Ethan Winn's picture

This could be especially useful for organizations with separate c3/c4 sites. The IRS requires the sites be clearly differentiated, so a popup that would inform users they were leaving one for the other would allow for copious linking while making the legal distinction clear.

Anonymous's picture

I was interested in your article. I'm pleased you are using JQuery - it is very reliable and convenient. JQuery - Library of JavaScript, focusing on the interaction between JavaScript and HTML. Library JQuery helps easy access to any part of the DOM, refer to the attributes and content of the DOM, to manipulate them. JQuery Library also provides a convenient API for working with Ajax.
Features of JQuery:

* Move the tree DOM, including support for XPath as a plug;
* Event;
* Visual effects;
* AJAX-applications;
* JavaScript-plugins.
This useful information about JQuery, I found torrents search engine.

Anonymous's picture

I was interested in your article. I'm pleased you are using JQuery - it is very reliable and convenient. JQuery - Library of JavaScript, focusing on the interaction between JavaScript and HTML. Library JQuery helps easy access to any part of the DOM, refer to the attributes and content of the DOM, to manipulate them. JQuery Library also provides a convenient API for working with Ajax.
Features of JQuery:

* Move the tree DOM, including support for XPath as a plug;
* Event;
* Visual effects;
* AJAX-applications;
* JavaScript-plugins.
This useful information about JQuery, I found torrents search engine.

oz's picture

Dude,this is way sweet.. Thanks and congrats.

Cash Advance Now's picture

Yes, thank you for this! I really appreciate all the internet help.

Andy Ford's picture

This is cool! I'm actually only using the custom :external selector and then using it with the cluetip plugin rather than jQuery UI, but the :external selector is brilliant!

I ran into a little hiccup on a site that is using the obtrusive "javascript:window.print();" as the href value. This was being treated as if it were an external link. So I added a check for "&& !obj.href.match(/^javascript\:/) "

The full custom selector code now looks like:
$.expr[':'].external = function(obj){
return !obj.href.match(/^mailto\:/)
&& !obj.href.match(/^javascript\:/)
&& (obj.hostname != location.hostname);
};

CasTex's picture

Well, first thanks for the code. I will try this in my host now.

Harubo's picture

Well, first thanks for the code. I will try this in my host now.

Harubo's picture

Well, first thanks for the code. I will try this in my host now.

Anonymous's picture

HADİ BAKALIM

hadi spam yok kocumdertghj kas kasadet sancısına ne iyi gelirhadi garierotik film izle - fersbuk - aşk falı - dertyli - - henbi fesbuk - gerdsacvbnm sıcak sohbet
- - - ferdsazx - gertyu -escort bayanlar hertfders - bedava film izle - gertyuliseli kızlar -fersd aşk şiirleri - merty çet -certylş şifalı bitkiler ve faydaları - gbnmöç. gtredsa - islami çet treds-playboy güzelleri bgtyuıop - çet hyuopğ jklşi
çet sohbet - hjklşhikayeler - htres çet sohbet asd
müzik dinle fgtre -sohbet - gbnmöçtürkü dinle jmöç
şarkı dinle ujklş
film izle uıkl - cinsel pozisyonlar grtyu
-frty sohbet netlog - kızlarla sohbet
rted- sohbet - gerkapak laflar - ert
peki doğum günü sözleri keri korku filmi izle hadi gari

Evie29's picture

I usually buy essay or pre written essays about this post*.

Anonymous's picture
home loans's picture

All people deserve wealthy life time and business loans or just secured loan would make it much better. Because people's freedom is based on money state.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockcode>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
Are you a robot? We usually like robots, but not in our comments.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.