-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsiveiframe.js
155 lines (141 loc) · 4.83 KB
/
responsiveiframe.js
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
/*! jQuery ResponsiveIframe - v0.0.3 - 2013-09-05
* https://github.com/npr/responsiveiframe
* Copyright (c) 2013 Irakli Nadareishvili; Licensed MIT, GPL */
if (typeof jQuery !== 'undefined') {
(function( $ ){
var settings = {
xdomain: '*',
ie : navigator.userAgent.toLowerCase().indexOf('msie') > -1,
scrollToTop: true
};
var methods = {
// initialization for the parent, the one housing this
init: function() {
return this.each(function(self){
var $this = $(this);
if (window.postMessage) {
if (window.addEventListener) {
window.addEventListener('message', function(e) {
privateMethods.messageHandler($this,e);
} , false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', function(e) {
privateMethods.messageHandler($this,e);
}, $this);
}
} else {
setInterval(function () {
var hash = window.location.hash, matches = hash.match(/^#h(\d+)(s?)$/);
if (matches) {
privateMethods.setHeight($this, matches[1]);
if (settings.scrollToTop && matches[2] === 's'){
scroll(0,0);
}
}
}, 150);
}
});
}
};
var privateMethods = {
messageHandler: function (elem, e) {
var height,
r,
matches,
strD;
if (settings.xdomain !== '*') {
var regex = new RegExp(settings.xdomain + '$');
if(e.origin == "null"){
throw new Error("messageHandler( elem, e): There is no origin. You are viewing the page from your file system. Please run through a web server.");
}
if(e.origin.match(regex)){
matches = true;
}else{
throw new Error("messageHandler( elem, e): The orgin doesn't match the responsiveiframe xdomain.");
}
}
if(settings.xdomain === '*' || matches ) {
strD = e.data + "";
r = strD.match(/^(\d+)(s?)$/);
if(r && r.length === 3){
height = parseInt(r[1], 10);
if (!isNaN(height)) {
try {
privateMethods.setHeight(elem, height);
} catch (ex) {}
}
if (settings.scrollToTop && r[2] === "s"){
scroll(0,0);
}
}
}
},
// Sets the height of the iframe
setHeight : function (elem, height) {
elem.css('height', height + 'px');
},
getDocHeight: function () {
var D = document;
return Math.min(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}
};
$.fn.responsiveIframe = function( method ) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
$.extend(settings, arguments[0]);
return methods.init.apply( this );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.responsiveIframe' );
}
};
}( jQuery ));
}
;(function(){
var self,
module,
ResponsiveIframe = function () {self = this;};
ResponsiveIframe.prototype.allowResponsiveEmbedding = function() {
if (window.addEventListener) {
window.addEventListener("load", self.messageParent, false);
window.addEventListener("resize", self.messageParent, false);
} else if (window.attachEvent) {
window.attachEvent("onload", self.messageParent);
window.attachEvent("onresize", self.messageParent);
}
this.checkDocumentHeight(self.messageParent);
};
ResponsiveIframe.prototype.checkDocumentHeight = function(callback){
var lastHeight = $(document).height(), newHeight, timer;
(function run(){
newHeight = $(document).height();
if( lastHeight != newHeight )
callback();
lastHeight = newHeight;
timer = setTimeout(run, 500);
})();
};
ResponsiveIframe.prototype.messageParent = function(scrollTop) {
var h = $(document).height();//document.body.offsetHeight;
//console.log(window.location.href +" - "+h+" - "+$('body').height()+" - "+$(document).height()+" - "+document.body.offsetHeight);
h = (scrollTop)? h+'s':h;
if(top.postMessage){
top.postMessage( h , '*');
} else {
window.location.hash = 'h'+h;
}
};
function responsiveIframe() {
return new ResponsiveIframe();
}
// expose
if ('undefined' === typeof exports) {
window.responsiveIframe = responsiveIframe;
} else {
module.exports.responsiveIframe = responsiveIframe;
}
}());