Created
December 22, 2015 18:27
-
-
Save DmitryDmitrienko/266f64ff5ca89e513921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BX.ready(function () { | |
(function ($) { | |
var options = { | |
'name_cookie': 'ivphone', | |
'default': ['+7 (8442) 33-27-45',"+7 (927) 510-43-50"], // телефон по-умолчанию | |
'selector': ['div.phones', '#iv-contacts'], // селектор для телефона по-умолчанию | |
// параметры | |
'parameters': { | |
'google': { // название для набора get параметров и телефонов | |
'get': { | |
'utm_source': 'GoogleAdwords' // get параметры (ключ и значение) | |
}, | |
'phone': ['+7 (8442) 50-32-35', '+7 (927) 510-32-35'], // список номеров телефонов | |
'selector': ['div.phones', '#iv-contacts'] //селектор для изменения телефона | |
}, | |
'yandex': { | |
'get': { | |
'utm_source': 'YandexDirect' | |
}, | |
'phone': ['+7 (8442) 50-32-35', '+7 (927) 510-32-35'], | |
'selector': ['div.phones', '#iv-contacts'] | |
} | |
} | |
}; | |
function setCookie(name, value, options) { | |
options = options || {}; | |
var expires = options.expires; | |
if (typeof expires == "number" && expires) { | |
var d = new Date(); | |
d.setTime(d.getTime() + expires * 1000); | |
expires = options.expires = d; | |
} | |
if (expires && expires.toUTCString) { | |
options.expires = expires.toUTCString(); | |
} | |
value = encodeURIComponent(value); | |
var updatedCookie = name + "=" + value; | |
for (var propName in options) { | |
updatedCookie += "; " + propName; | |
var propValue = options[propName]; | |
if (propValue !== true) { | |
updatedCookie += "=" + propValue; | |
} | |
} | |
document.cookie = updatedCookie; | |
} | |
function getCookie(name) { | |
var matches = document.cookie.match(new RegExp( | |
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" | |
)); | |
return matches ? decodeURIComponent(matches[1]) : undefined; | |
} | |
function deleteCookie(name) { | |
setCookie(name, "", { | |
expires: -1 | |
}); | |
} | |
function getParameterByName(name) { | |
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
results = regex.exec(location.search); | |
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
function changePhone(selectors, val){ | |
for(var index = 0; index < selectors.length; index++){ | |
console.log(selectors[index]); | |
$(selectors[index]).html(val); | |
} | |
} | |
function main() { | |
var isOk = true; | |
outer: for (var key in options.parameters) { | |
var param_phone = options.parameters[key]; | |
var s = $(param_phone.selector); | |
var text_phone = ''; | |
for (var g in param_phone.get) { | |
if (getParameterByName(g) !== param_phone.get[g]) isOk = false; | |
else isOk = true; | |
} | |
// работа с GET парамтетрами | |
if (isOk) { | |
for (var p in param_phone.phone) { | |
text_phone = text_phone + param_phone.phone[p] + '<br>'; | |
} | |
changePhone(param_phone.selector,text_phone); | |
setCookie(options.name_cookie, text_phone, {'expires' : 86400*7}); | |
} | |
// смотрим в куках | |
if (!isOk) { | |
text_phone = getCookie(options.name_cookie); | |
if (text_phone != undefined) { | |
isOk = true; | |
changePhone(param_phone.selector, text_phone); | |
} | |
else{ | |
isOk = false; | |
} | |
} | |
// если в GET параметрах был полный набор | |
// или была информация в куках | |
// останавливаем цикл | |
if(isOk){ | |
break outer; | |
} | |
} | |
var default_phone = ''; | |
for (var p in options.default) { | |
default_phone = default_phone + options.default[p] + '<br>'; | |
} | |
// если ни один вид get параметров не подошёл | |
// устанавливаем значение из настроек по-умолчанию | |
if(!isOk) changePhone(options.selector, default_phone); | |
} | |
main(); | |
})(jQuery); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment