How to stop domain-wide global JavaScript from executing on specific landing pages


We run Live Chat as a domain-wide global JavaScript across all our landing pages.

Now, we get traffic from a few different lead providers running email campaigns and co-registration campaigns for us.  

As most of these are pay per lead, they don’t like us having Live Chat on our landing pages for them.  Whereas we definitely want LC on all our PPC campaign pages; AdWords, Bing, Facebook Ads, etc.

So, I was faced with having to disable the global chat JS and place it into 90% of our pages, leaving it off only the pay per lead provider pages, or working out a method to exclude those specific pages from loading the Live Chat script.

Here’s the simple test code I came up with.

Can anyone suggest improvements or point to any potential problems?

So let’s say you need to exclude provider1 and provider2 and you have their landing pages all containing string1 and string2 in the URLs.  That’s what we do.

The code wrapper looks like this…

 


2 replies

I should clarify this is actually working in production… just wanted to know if I needed to do anything else with it.

More general approach to handle the query string with JS:

include this in the HEAD:

(function () {

'use strict';

var queryString = {};

  queryString.parse = function (str) {

 if (typeof str !== 'string') {

  return {};

 }



str = str.trim().replace(/^(\?|#)/, '');



if (!str) {

  return {};

 }



return str.trim().split('&').reduce(function (ret, param) {

  var parts = param.replace(/\+/g, ' ').split('=');

  var key = parts[0];

  var val = parts[1];

  key = decodeURIComponent(key);

  val = val === undefined ? null : decodeURIComponent(val);

  if (!ret.hasOwnProperty(key)) {



ret[key] = val;

  } else if (Array.isArray(ret[key])) {



ret[key].push(val);

  } else {



ret[key] = [ret[key], val];

  }

  return ret;

 }, {});

};

  if (typeof define === 'function' && define.amd) {

 define([], queryString);

} else if (typeof module !== 'undefined' && module.exports) {

 module.exports = queryString;

} else {

 window.queryString = queryString;

} })();

Now the usage is:

var q = queryString.parse( location.search );

if (typeof q.whatever !== 'undefined') {     //querystring variable is present} else {     //querystring variable is NOT present}

Reply