diff --git a/openx.inc b/openx.inc
index d53a37a..b8a4fa3 100644
--- a/openx.inc
+++ b/openx.inc
@@ -22,15 +22,10 @@ function _openx_javascript() {
     return;
   }
 
-  if (!empty($_SERVER['HTTPS'])) {
-    $protocol = 'https';
-    $server = variable_get('openx_delivery_url_https', 'd.openx.org');
-  }
-  else {
-    $protocol = 'http';
-    $server = variable_get('openx_delivery_url', 'd.openx.org');
-  }
-  $url = $protocol . '://' . trim($server, '/') . '/spcjs.php';
+
+  $httpsserver = trim(variable_get('openx_delivery_url_https', 'd.openx.org'), '/');
+  $httpserver = trim(variable_get('openx_delivery_url', 'd.openx.org'), '/');
+  $url = "\" + ('https:' == document.location.protocol ? 'https://$httpsserver' : 'http://$httpserver')+" . '"/spcjs.php';
   $vars = array();
   if ($site_vars = variable_get('openx_site_vars', FALSE)) {
     if (module_exists('token')) {
@@ -97,7 +92,7 @@ function _openx_javascript() {
   $script .= '   return "";';
   $script .= '}';
 
-  $script .= "document.write(unescape(\"%3Cscript src='";
+  $script .= "document.write(unescape(\"%3Cscript src=";
   $script .= $url;
   $script .= "'%3E%3C/script%3E\"));";
 
