(function ($, Drupal, drupalSettings) { 'use strict'; Drupal.behaviors.externalmodalbehavior = { attach: function (context, settings) { $('body', context).once('external-modal').each(function () { var externalUrl = null; // Open external links in a popup modal $( 'a .external-link' ).each(function() { if( location.hostname === this.hostname || !this.hostname.length ) { return; } else { $(this).attr('target', '_blank'); } }); $(document).on('click', '.external-link', function handleExternalUrl(event) { // prevent link from opening event.preventDefault(); // open a modal $('.external-modal').css('visibility', 'visible'); $('body').css('overflow', 'hidden'); //go to link on modal close externalUrl = $(this).attr('href'); }); // open link on continue click $('.btn-modal.btn-continue').click(function() { window.open(externalUrl); $('.external-modal').css('visibility', 'hidden'); $('body').css('overflow', 'auto'); return false; }); // close modal on button close click $('.btn-modal.btn-close').click(function() { $('.external-modal').css('visibility', 'hidden'); $('body').css('overflow', 'auto'); return false; }); // close modal on escape keyup $(document).keyup(function(e) { if (e.keyCode === 27) { $('.external-modal').css('visibility', 'hidden'); $('body').css('overflow', 'auto'); } return false; }); }); } } })(jQuery, Drupal, drupalSettings);