bitwix

Tangential comments about Software Development

Monday, January 28, 2013

PHP writing JavaScript writing HTML

I look after a community website, using the Typo3 Content Management System. The CMS tries to be a front end to achieve what you want without understanding HTML, but I found it sometimes gets in the way. When I was asked to have randomly selected banners with different clickthroughs, I couldn't see a way that the CMS supported that. I write some PHP, but because the pages are cached I only ever got the same random advert. The solution was PHP writing JavaScript writing HTML. Never again!



$contentBefore = $this->cObjGetSingle($conf["cObj"],$conf["cObj."]);
$content = $contentBefore.'<script type="text/javascript">'
  .'var src, url, ajcidx = Math.floor(Math.random() * 2),html;'
  .'if( ajcidx == 0 ){'
  .'url = \'<a href="http://www.advertiser1.com" target="_blank">\';'
  .'src = \'<img src="/images/advert1.gif" width="750" height="225" alt="banner">\';'
  .'}else{'
  .'url = \'<a href="http://www.advertiser2.com" target="_blank">\';'
  .'src = \'<img src="/images/advert2.gif" width="753" height="224" alt="banner">\';'
  .'}'
  .'document.write( url );'
  .'document.write( src );'
  .'document.write( "</a>" );'
  .'</script>';
$content = $this->stdWrap($content,$conf["stdWrap."]);