diff --git a/sites/default/modules/ad/ad.module b/sites/default/modules/ad/ad.module
index 8f97c4d..43e02a2 100644
--- a/sites/default/modules/ad/ad.module
+++ b/sites/default/modules/ad/ad.module
@@ -157,7 +157,7 @@ function ad($group = FALSE, $quantity = 1, $options = array()) {
       else {
         $query['u'] = $_GET['q'];
       }
-      $src = htmlentities(url($base_url .'/'. $adserve, array('query' => $query)));
+      $src = url($base_url .'/'. $adserve, array('query' => $query));
       if ($options['ad_display'] == 'iframe') {
         // TODO: We need to know the IFrame size before it is displayed.  This
         // limits the flexibility of what can be displayed in these frames.
@@ -172,14 +172,14 @@ function ad($group = FALSE, $quantity = 1, $options = array()) {
         if ($width = variable_get('ad_iframe_width', '')) {
           $append .= 'width="'. $width .'" ';
         }
-        $output = '<iframe src="'. $src ."\" $append></iframe>";
+        $output = '<iframe src="'. htmlentities($src) ."\" $append></iframe>";
       }
       else if ($options['ad_display'] == 'jquery') {
         // The theme function uses this to generate a CSS id for jQuery to use.
         $output = $src;
       }
       else {
-        $output = "<script type='text/javascript' src='$src'></script>";
+        $output = "<script type='text/javascript' src='". htmlentities($src) ."'></script>";
       }
       break;
   }
@@ -211,7 +211,6 @@ function theme_ad_display($group, $display, $method = 'javascript') {
   $group = preg_replace('/[,]/', '+', $group);
 
   if ($method == 'jquery') {
-    drupal_add_js('misc/jquery.js', 'core');
     return "\n<div class=\"advertisement group-$group\" id=\"group-id-$id\">\n <script type=\"text/javascript\">\n//<![CDATA[\n  $(document).ready(function(){ jQuery(\"div#group-id-$id\").load(\"$display\"); });\n //]]>\n </script>\n</div>\n";
   }
   else if ($method == 'raw') {
@@ -443,6 +442,18 @@ function theme_ad_status_display($node) {
 }
 
 /**
+ * Implementation of hook_init.
+ */
+function ad_init() {
+  // We do this in the init hook so that it doesn't get skipped when block
+  // caching is enabled.
+  $method = variable_get('ad_display', 'javascript');
+  if ($method == 'jquery') {
+    drupal_add_js('misc/jquery.js', 'core');
+  }
+}
+
+/**
  * Implementation of hook_help().
  */
 function ad_help($path, $arg) {
