1
|
/**
|
2
|
* http://molily.de/js/
|
3
|
* Cross browser addEvent function by John Resig
|
4
|
* http://ejohn.org/blog/flexible-javascript-events/
|
5
|
* some samples
|
6
|
* addEvent( document.getElementById('foo'), 'click', doSomething );
|
7
|
* addEvent( obj, 'mouseover', function(){ alert('hello!'); } );
|
8
|
*
|
9
|
*/
|
10
|
/**
|
11
|
* Cross Browser helper to addEventListener.
|
12
|
* http://webintersect.com/articles/72/add-event-listener-to-dynamic-elements
|
13
|
*
|
14
|
* @param {HTMLElement} obj The Element to attach event to.
|
15
|
* @param {string} evt The event that will trigger the binded function.
|
16
|
* @param {function(event)} fnc The function to bind to the element.
|
17
|
* @return {boolean} true if it was successfuly binded.
|
18
|
*/
|
19
|
var addEvent = function (obj, evt, fnc) {
|
20
|
// W3C model
|
21
|
if (obj.addEventListener) {
|
22
|
obj.addEventListener(evt, fnc, false);
|
23
|
return true;
|
24
|
}
|
25
|
// Microsoft model
|
26
|
else if (obj.attachEvent) {
|
27
|
return obj.attachEvent('on' + evt, fnc);
|
28
|
}
|
29
|
// Browser don't support W3C or MSFT model, go on with traditional
|
30
|
else {
|
31
|
evt = 'on' + evt;
|
32
|
if (typeof obj[evt] === 'function') {
|
33
|
// Object already has a function on traditional
|
34
|
// Let's wrap it with our own function inside another function
|
35
|
fnc = (function (f1, f2) {
|
36
|
return function () {
|
37
|
f1.apply(this, arguments);
|
38
|
f2.apply(this, arguments);
|
39
|
}
|
40
|
}) (obj[evt], fnc);
|
41
|
}
|
42
|
obj[evt] = fnc;
|
43
|
return true;
|
44
|
}
|
45
|
return false;
|
46
|
};
|
47
|
/*****************************************************************************/
|
48
|
/**
|
49
|
* sample
|
50
|
* removeEvent( object, eventType, function );
|
51
|
*
|
52
|
*/
|
53
|
function removeEvent(obj, ev, fn) {
|
54
|
if (obj.detachEvent) {
|
55
|
obj.detachEvent('on' + ev, obj[ev + fn]);
|
56
|
obj[ev + fn] = null;
|
57
|
} else
|
58
|
obj.removeEventListener(ev, fn, false);
|
59
|
}
|
60
|
/*****************************************************************************/
|
61
|
|
62
|
var getBrowser = (function () {
|
63
|
var navigatorObj = navigator.appName,
|
64
|
userAgentObj = navigator.userAgent,
|
65
|
matchVersion;
|
66
|
var match = userAgentObj.match(/(opera|opr|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
|
67
|
if (match && (matchVersion = userAgentObj.match(/version\/([\.\d]+)/i)) !== null) {
|
68
|
match[2] = matchVersion[1];
|
69
|
}
|
70
|
//mobile
|
71
|
|
72
|
if (navigator.userAgent.match(/iPhone|Android|webOS|iPad/i)) {
|
73
|
return match ? [
|
74
|
match[1],
|
75
|
match[2],
|
76
|
mobile
|
77
|
] : [
|
78
|
navigatorObj,
|
79
|
navigator.appVersion,
|
80
|
mobile
|
81
|
];
|
82
|
}
|
83
|
// web browser
|
84
|
|
85
|
return match ? [
|
86
|
match[1],
|
87
|
match[2]
|
88
|
] : [
|
89
|
navigatorObj,
|
90
|
navigator.appVersion,
|
91
|
'-?'
|
92
|
];
|
93
|
}) ();
|
94
|
// forEach method, could be shipped as part of an Object Literal/Module
|
95
|
var forEach = function (array, callback, scope) {
|
96
|
for (var i = 0; i < array.length; i++) {
|
97
|
callback.call(scope, i, array[i]); // passes back stuff we need
|
98
|
}
|
99
|
};
|
100
|
function each(elm, fn) {
|
101
|
for (var i = 0, l = elm.length; i < l; i++) {
|
102
|
fn.call(elm, elm[i], i);
|
103
|
}
|
104
|
}
|
105
|
function doSomething(elm) {
|
106
|
if ((typeof elm !== 'undefined') || elm) console.log(elm);
|
107
|
}
|
108
|
/**
|
109
|
* http://www.axel-hahn.de/blog/2015/01/21/javascript-schnipsel-html-strippen/
|
110
|
*/
|
111
|
|
112
|
function strip_tags(s) {
|
113
|
return s.replace(/<[^>]*>/g, '');
|
114
|
}
|
115
|
/**
|
116
|
* discuss at: http:phpjs.org/functions/dirname/
|
117
|
* http: kevin.vanzonneveld.net
|
118
|
* original by: Ozh
|
119
|
* improved by: XoraX (http:www.xorax.info)
|
120
|
* example 1: dirname('/etc/passwd');
|
121
|
* returns 1: '/etc'
|
122
|
*/
|
123
|
|
124
|
var dirname = function (path) {
|
125
|
var tmp = path.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, '');
|
126
|
return tmp;
|
127
|
};
|
128
|
/**
|
129
|
* http://durhamhale.com/blog/javascript-version-of-phps-str-replace-function
|
130
|
*/
|
131
|
var str_replace = function (search, replace, string) {
|
132
|
return string.split(search).join(replace);
|
133
|
};
|
134
|
/**
|
135
|
* trim, rtrim, ltrim
|
136
|
* http://coursesweb.net/javascript/trim-rtrim-ltrim-javascript_cs
|
137
|
*/
|
138
|
var trim = function (str, chr) {
|
139
|
var rgxtrim = (!chr) ? new RegExp('^\\s+|\\s+$', 'g') : new RegExp('^' + chr + '+|' + chr + '+$', 'g');
|
140
|
return str.replace(rgxtrim, '');
|
141
|
};
|
142
|
var rtrim = function (str, chr) {
|
143
|
var rgxtrim = (!chr) ? new RegExp('\\s+$') : new RegExp(chr + '+$');
|
144
|
return str.replace(rgxtrim, '');
|
145
|
};
|
146
|
var ltrim = function (str, chr) {
|
147
|
var rgxtrim = (!chr) ? new RegExp('^\\s+') : new RegExp('^' + chr + '+');
|
148
|
return str.replace(rgxtrim, '');
|
149
|
};
|
150
|
var confirm_link = function (message, url) { // class="alert rounded"
|
151
|
if (confirm(message)) location.href = url;
|
152
|
};
|
153
|
var showMessage = (function (txt, sel) {
|
154
|
var result = window.document.getElementById('messages');
|
155
|
if (!result) {
|
156
|
return false;
|
157
|
}
|
158
|
var elm = document.createElement('P');
|
159
|
elm.setAttribute('class', sel + ' rounded');
|
160
|
elm.appendChild(document.createTextNode(txt));
|
161
|
result.appendChild(elm);
|
162
|
});
|
163
|
/**
|
164
|
* http://www.javascriptkit.com/dhtmltutors/treewalker.shtml
|
165
|
*
|
166
|
*/
|
167
|
/********************************************************************************************************/
|
168
|
var LoadOnFly = (function (nodeName, file) {
|
169
|
'use strict';
|
170
|
if (typeof file === 'undefined') {
|
171
|
return false;
|
172
|
}
|
173
|
// console.info(document.doctype);
|
174
|
if (!document.doctype) {
|
175
|
return false;
|
176
|
}
|
177
|
/*
|
178
|
var nodeDoctype = document.implementation.createDocumentType(
|
179
|
'html','',''
|
180
|
);
|
181
|
document.replaceChild(nodeDoctype, document.doctype);
|
182
|
} else {
|
183
|
document.insertBefore(nodeDoctype, document.childNodes[0]);
|
184
|
}
|
185
|
*/
|
186
|
// var LoadOnFly = function (nodeName, url) {
|
187
|
|
188
|
var jsRegex = /.js$/gi;
|
189
|
var cssRegex = /.css$/gi;
|
190
|
var scripts = {
|
191
|
};
|
192
|
// console.info(' 0.' + file );fileExtension = file.replace(/^.*\./, '');
|
193
|
var url = file;
|
194
|
var urlExt = trim(file.replace(/^.*\./, ''));
|
195
|
var NodeList = null;
|
196
|
var len = 0;
|
197
|
var node = null;
|
198
|
var str = 'undefined';
|
199
|
var done = false;
|
200
|
//console.info( urlExt + ' = 1.) ' + url);
|
201
|
if ((typeof url !== 'undefined') && (urlExt === 'js')) {
|
202
|
// console.info(urlExt + ' = 1.) ' + url);
|
203
|
scripts[url] = false;
|
204
|
switch (nodeName) {
|
205
|
case 'body':
|
206
|
NodeList = document.body.querySelectorAll('SCRIPT');
|
207
|
break;
|
208
|
default:
|
209
|
NodeList = document.head.querySelectorAll('SCRIPT');
|
210
|
break;
|
211
|
}
|
212
|
if (NodeList) {
|
213
|
len = NodeList.length - 1;
|
214
|
}
|
215
|
//console.info(NodeList);
|
216
|
// console.info(' JS ' + url);
|
217
|
|
218
|
try {
|
219
|
var js = document.createElement('SCRIPT');
|
220
|
js.setAttribute('type', 'text/javascript'); // optional, if not a html5 node
|
221
|
js.setAttribute('src', url); // src setzen
|
222
|
js.setAttribute('charset', 'UTF-8');
|
223
|
// js.setAttribute("async", true); // HTML5 Asyncron attribute
|
224
|
done = false;
|
225
|
if (nodeName == 'body') {
|
226
|
node = window.document.body.querySelectorAll('SCRIPT') [len];
|
227
|
node.parentNode.appendChild(js);
|
228
|
// script.parentNode.insertBefore(js,script);
|
229
|
} else {
|
230
|
node = window.document.head.querySelectorAll('SCRIPT') [len];
|
231
|
node.parentNode.appendChild(js);
|
232
|
}
|
233
|
} catch (e) {
|
234
|
var str = '<script type=\'text/javascript\' src=\'' + url + '\' charset="UTF-8"><' + '/script>';
|
235
|
document.write(str);
|
236
|
}
|
237
|
}
|
238
|
// load css only within head
|
239
|
|
240
|
if ((typeof url !== 'undefined') && (urlExt === 'css')) {
|
241
|
//console.info(urlExt + ' = 2.) ' + url);
|
242
|
scripts[url] = false;
|
243
|
try {
|
244
|
var css = document.createElement('link'),
|
245
|
len = 0;
|
246
|
css.setAttribute('type', 'text/css');
|
247
|
css.setAttribute('rel', 'stylesheet');
|
248
|
css.setAttribute('media', 'all');
|
249
|
css.setAttribute('href', url);
|
250
|
NodeList = window.document.querySelectorAll('LINK');
|
251
|
if (NodeList) {
|
252
|
len = NodeList.length - 1;
|
253
|
}
|
254
|
//console.info(NodeList);
|
255
|
// insert after last link element if exist otherwise before first script
|
256
|
if (len > - 1) {
|
257
|
node = window.document.head.querySelectorAll('LINK') [len];
|
258
|
// console.info( len );
|
259
|
// console.info(node);
|
260
|
// return false;
|
261
|
node.parentNode.insertBefore(css, node.nextSibling);
|
262
|
// console.info('CSS ' + url);
|
263
|
} else {
|
264
|
node = window.document.head.querySelectorAll('SCRIPT') [0];
|
265
|
node.parentNode.insertBefore(css, node);
|
266
|
}
|
267
|
} catch (e) {
|
268
|
str = '<link href=\'' + url + '\' media="all" rel="stylesheet" />';
|
269
|
document.write(str);
|
270
|
}
|
271
|
}
|
272
|
// console.info( url );
|
273
|
// showMessage(url);
|
274
|
|
275
|
});
|
276
|
/**
|
277
|
*
|
278
|
document.onreadystatechange = function () {
|
279
|
if (document.readyState == "interactive") {
|
280
|
console.info( 'Start readyState.interactive' );
|
281
|
}
|
282
|
}
|
283
|
|
284
|
// Alternativ zu load event
|
285
|
document.onreadystatechange = function () {
|
286
|
if (document.readyState == "complete") {
|
287
|
console.info( 'Start readyState.complete' );
|
288
|
}
|
289
|
}
|
290
|
|
291
|
|
292
|
window.onload = function() {
|
293
|
addEvent(document, "DOMContentLoaded", LoadOnFly);
|
294
|
console.info( 'Start window.onload' );
|
295
|
};
|
296
|
*/
|
297
|
/*
|
298
|
undefined
|
299
|
*/
|