Adding elements to the head of a MODx page

To add some code to the head of an html page you just need a few lines of code. This saves loading a style sheet or javascript on every page or adding a snippet to the head of a template.

To add some JavaScript

$js = '<script src="/assets/js/jquery.accordion.js" type="text/javascript"></script>'
$modx->regClientStartupScript($js);

~ or ~

$js = "assets/js/prototype.js";
$modx->regClientStartupScript($js);

~ or ~

$js = '<script type="text/javascript">$(document).ready(function() { } </script>';
$modx->regClientStartupScript($js);

To add a stylesheet

$css = '<link href="/assets/site/styles.css" rel="stylesheet" type="text/css" media="all" />';
$modx->regClientCSS( $css );

~ or ~

$css = "/assets/site/styles.css";
$modx->regClientCSS($css);

~ or ~

$css = '<style media="screen">* { margin:0; padding:0 }</style>';
$modx->regClientCSS($css);

To add any HTML

$html = '<meta http-equiv="refresh" content="5;URL=http:www.yoursite.com/landingpage.html">';
$modx->regClientStartupHTMLBlock($html);

About Keiron

Web Developer based in the UK. Click here if you want to work with me