Hi, while working on #2468521: Option for SSL path tracking file, I would like to substitute the following javascript snippet from a tracking company:

var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" +
scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");

with something more in tune with Drupal. Can I check for https with the variable $is_https instead, and are there any side-effects to be aware of (such as when working with loadbalancers etc)?

eg:

    $link = 'statcounter.com/counter/counter.js';
    $url_scheme = ($is_https) ? 'https://secure.' : 'http://www.';
    $correct_link = $url_scheme . $link;

Thanks

Comments

jaypan’s picture

You can pass the protocol as a setting to your JS:

global $is_https;
drupal_add_js('myModule' => array('protocol' => ($is_https ? 'https' : 'http')), 'setting');

Then you have access in your JS script to the protocol at:

Drupal.settings.myModule.protocol

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
kdebaas’s picture

Thanks. I ended up passing the whole url rather than just the protocol to JS, which comes down to the same thing, I suppose.