background image

Content tagged with: hostname

Eric's picture

This quick code snippet will show you how to update every external link on your site to open in a new window using jQuery. You can put this code in the script.js file in your theme.

$(document).ready(function(){

  // apply to all <a> tags that have an href that starts with "http"
  $("a[href^='http']").each(function(){

    // remove http:// and https://
    link = $(this).attr('href');
    if (link.substring(0,7)=='http://') {
      link = link.substring(7);
    } else if (link.substring(0,8)=='https://') {
      link = link.substring(8);
    } else {
      return;
    }

    // spilt on '/'
    split1 = link.split('/');

    // compare href hostname to site hostname
    if (split1[0]!=location.hostname) {
      // add target attribute to link
      $(this).attr('target','_blank');
    }

  });

});

Eric's picture

If you're running a multi-site Drupal configuration, it's helpful to know which settings.php is being loaded. This information may be useful to your module or theme, or for troubleshooting purposes. For instance, you might be sharing the same theme across a few sites, but you'd like to modify the theme for one of your sites. Sharing themes (and modules) is capable in a multi-site configuration by placing your themes and modules in the [sites/all] folder, while defining a directory and settings.php file for each site installation in [sites]. To be able to tell which settings.php is being loaded, you could add the following code to each settings.php file you define:

<?php
$exploded
= explode("/", __FILE__);
$conf['multi_site_hostname'] = $exploded[count($exploded)-2];
?>

For example, you could pass this information to your page.tpl theme file:

<?php
// defining a preprocess_page function, placed in my template.php file:
function MYTHEME_preprocess_page(&$variables) {
 
// example: pass the configuration directly to the page.tpl.php:
 
$variables['multi_site_hostname'] = variable_get('multi_site_hostname',NULL);

 
// another example:
 
switch (variable_get('multi_site_hostname',NULL)) {
    case
'thedrupalblog.erl.dev':
    case
'thedrupalblog.com':
     
$variables['siteHostName'] = 'thedrupalblog.com';
      break;
    default:
     
$variables['siteHostName'] = 'default';
      break;
  }

}
?>