Snippets
Created by
Oleg Korsak
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | (function () {
"use strict";
if (document.getElementsByTagName('waavo:iframe')) {
function parseIframeUrl(val) {
val = val.replace("?", "").split('&');
for (var i = 0; i < val.length; i++) {
var val2 = val[i].split('=');
if (val2[0] == 'wurl') {
return val2[1];
}
}
return '';
}
var elem = document.getElementsByTagName('waavo:iframe');
for (elemi = 0; elemi < elem.length; elemi++) {
var project = elem[elemi].getAttribute('project');
var host = 'waavo.lt';
var http = 'http';
var https = true;
if (elem[elemi].getAttribute('https')) {
https = elem[elemi].getAttribute('https');
}
if (elem[elemi].getAttribute('host')) {
host = elem[elemi].getAttribute('host');
}
var url = elem[elemi].getAttribute('url');
if (parseIframeUrl(window.location.search)) {
url = parseIframeUrl(window.location.search);
}
var insertTo = 'waavo-insert-iframe';
if (elem[elemi].getAttribute('insert-iframe-id')) {
insertTo = elem[elemi].getAttribute('insert-iframe-id');
}
if (url == null) {
url = '/';
}
var iframeUrl = project + '.' + host + url;
if (elem[elemi].getAttribute('language')) {
if (iframeUrl.indexOf('?') != -1) {
iframeUrl += '&language=' + elem[elemi].getAttribute('language');
} else {
iframeUrl += '?language=' + elem[elemi].getAttribute('language');
}
}
if (elem[elemi].getAttribute('currency')) {
if (iframeUrl.indexOf('?') != -1) {
iframeUrl += '¤cy=' + elem[elemi].getAttribute('currency');
} else {
iframeUrl += '?currency=' + elem[elemi].getAttribute('currency');
}
}
if (host == 'waavo.com') {
http = 'https';
}
if (https == 'false') {
http = 'http';
}
document.getElementById(insertTo).innerHTML = '<iframe id="waavoiframe' + elemi + '" src="' + http + '://' + iframeUrl + '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:262px;" allowTransparency="true"></iframe><div style="clear:both"></div>';
}
function changeUrl(url) {
url = url.replace('#', '');
var iframe = document.getElementById('waavoiframe');
iframe.setAttribute('src', http + '://' + project + '.' + host + url);
}
var XD = function () {
var interval_id, attached_callback, window = this;
return {
receiveMessage: function (callback, source_origin) {
/* browser supports window.postMessage */
if (window['postMessage']) {
/* bind the callback to the actual event associated with window.postMessage */
if (callback) {
attached_callback = function (e) {
if (( typeof source_origin === 'string' && e.origin !== source_origin) || (Object.prototype.toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
return !1;
}
callback(e);
};
}
if (window['addEventListener']) {
window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
} else {
window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
}
} else {
/* a polling loop is started & callback is called whenever the location.hash changes */
interval_id && clearInterval(interval_id);
interval_id = null;
if (callback) {
interval_id = setInterval(function () {
var hash = document.location.hash, re = /^#?\d+&/;
if (hash !== last_hash && re.test(hash)) {
last_hash = hash;
callback({
data: hash.replace(re, '')
});
}
}, 100);
}
}
}
};
}();
XD.receiveMessage(function (data) {
if (data.data.substr(0, 1) != '{') {
return false;
}
var frames = document.getElementsByTagName('iframe');
var elementId = null;
for (var i = 0; i < frames.length; i++) {
if (frames[i].contentWindow === data.source) {
elementId = frames[i].id;
break;
}
}
if(!elementId) {
return false;
}
data = eval('(' + data.data + ')');
data = data.data;
if (!data) {
return false;
}
// updates iframe height
if (data.height) {
changeFrameHeight(data.height, elementId);
}
if (data.scrollToTop) {
scrollToIframeTop();
}
if (data.url) {
if (window.history.pushState) {
url = replaceUrlParam(window.location.href, 'wurl', data.url);
window.history.pushState('page2', 'Title', url);
}
scroll(0, 0);
}
});
function changeFrameHeight(height, id) {
document.getElementById(id).style.height = height + 'px';
}
function scrollToIframeTop() {
document.getElementById('waavo-insert-iframe').scrollIntoView();
}
function replaceUrlParam(url, paramName, paramValue) {
var pattern = new RegExp('\\b(' + paramName + '=).*?(&|$)');
if (url.search(pattern) >= 0) {
return url.replace(pattern, '$1' + paramValue + '$2');
}
return url + (url.indexOf('?') > 0 ? '&' : '?') + paramName + '=' + paramValue
}
}
}());
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.