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
|
var mobile;
|
74
|
return match ? [
|
75
|
match[1],
|
76
|
match[2],
|
77
|
mobile
|
78
|
] : [
|
79
|
navigatorObj,
|
80
|
navigator.appVersion,
|
81
|
mobile
|
82
|
];
|
83
|
}
|
84
|
// web browser
|
85
|
|
86
|
return match ? [
|
87
|
match[1],
|
88
|
match[2]
|
89
|
] : [
|
90
|
navigatorObj,
|
91
|
navigator.appVersion,
|
92
|
'-?'
|
93
|
];
|
94
|
}) ();
|
95
|
// forEach method, could be shipped as part of an Object Literal/Module
|
96
|
var forEach = function (array, callback, scope) {
|
97
|
for (var i = 0; i < array.length; i++) {
|
98
|
callback.call(scope, i, array[i]); // passes back stuff we need
|
99
|
}
|
100
|
};
|
101
|
function each(elm, fn) {
|
102
|
for (var i = 0, l = elm.length; i < l; i++) {
|
103
|
fn.call(elm, elm[i], i);
|
104
|
}
|
105
|
}
|
106
|
function doSomething(elm) {
|
107
|
if ((typeof elm !== 'undefined') || elm) console.log(elm);
|
108
|
}
|
109
|
/**
|
110
|
* http://www.axel-hahn.de/blog/2015/01/21/javascript-schnipsel-html-strippen/
|
111
|
*/
|
112
|
|
113
|
function strip_tags(s) {
|
114
|
return s.replace(/<[^>]*>/g, '');
|
115
|
}
|
116
|
/**
|
117
|
* discuss at: http:phpjs.org/functions/dirname/
|
118
|
* http: kevin.vanzonneveld.net
|
119
|
* original by: Ozh
|
120
|
* improved by: XoraX (http:www.xorax.info)
|
121
|
* example 1: dirname('/etc/passwd');
|
122
|
* returns 1: '/etc'
|
123
|
*/
|
124
|
|
125
|
var dirname = function (path) {
|
126
|
var tmp = path.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, '');
|
127
|
return tmp;
|
128
|
};
|
129
|
/**
|
130
|
* http://durhamhale.com/blog/javascript-version-of-phps-str-replace-function
|
131
|
*/
|
132
|
var str_replace = function (search, replace, string) {
|
133
|
return string.split(search).join(replace);
|
134
|
};
|
135
|
/**
|
136
|
* trim, rtrim, ltrim
|
137
|
* http://coursesweb.net/javascript/trim-rtrim-ltrim-javascript_cs
|
138
|
*/
|
139
|
var trim = function (str, chr) {
|
140
|
var rgxtrim = (!chr) ? new RegExp('^\\s+|\\s+$', 'g') : new RegExp('^' + chr + '+|' + chr + '+$', 'g');
|
141
|
return str.replace(rgxtrim, '');
|
142
|
};
|
143
|
var rtrim = function (str, chr) {
|
144
|
var rgxtrim = (!chr) ? new RegExp('\\s+$') : new RegExp(chr + '+$');
|
145
|
return str.replace(rgxtrim, '');
|
146
|
};
|
147
|
var ltrim = function (str, chr) {
|
148
|
var rgxtrim = (!chr) ? new RegExp('^\\s+') : new RegExp('^' + chr + '+');
|
149
|
return str.replace(rgxtrim, '');
|
150
|
};
|
151
|
var confirm_link = function (message, url) { // class="alert rounded"
|
152
|
if (confirm(message)) location.href = url;
|
153
|
};
|
154
|
var showMessage = (function (txt, sel) {
|
155
|
var result = window.document.getElementById('messages');
|
156
|
if (!result) {
|
157
|
return false;
|
158
|
}
|
159
|
var elm = document.createElement('P');
|
160
|
elm.setAttribute('class', sel + ' rounded');
|
161
|
elm.appendChild(document.createTextNode(txt));
|
162
|
result.appendChild(elm);
|
163
|
});
|
164
|
|
165
|
/**
|
166
|
* http://www.javascriptkit.com/dhtmltutors/treewalker.shtml
|
167
|
*
|
168
|
*/
|
169
|
/********************************************************************************************************/
|
170
|
var LoadOnFly = (function ( nodeName, file ) {
|
171
|
'use strict';
|
172
|
if( (typeof file === 'undefined') ) {
|
173
|
return false;
|
174
|
}
|
175
|
if ( !document.doctype ) {
|
176
|
return false;
|
177
|
}
|
178
|
/*
|
179
|
var nodeDoctype = document.implementation.createDocumentType(
|
180
|
'html','',''
|
181
|
);
|
182
|
document.replaceChild(nodeDoctype, document.doctype);
|
183
|
} else {
|
184
|
document.insertBefore(nodeDoctype, document.childNodes[0]);
|
185
|
}
|
186
|
*/
|
187
|
// var LoadOnFly = function (nodeName, url) {
|
188
|
|
189
|
var jsRegex = /.js$/gi;
|
190
|
var cssRegex = /.css$/gi;
|
191
|
var scripts = {
|
192
|
};
|
193
|
// console.info(' 0.' + file );fileExtension = file.replace(/^.*\./, '');
|
194
|
var url = file;
|
195
|
var urlExt = trim(file.replace(/^.*\./, ''));
|
196
|
var NodeList = null;
|
197
|
var len = 0;
|
198
|
var node = null;
|
199
|
var str = 'undefined';
|
200
|
var done = false;
|
201
|
//console.info( urlExt + ' = 1.) ' + url);
|
202
|
if ((typeof url !== 'undefined') && (urlExt === 'js')) {
|
203
|
// console.info(urlExt + ' = 1.) ' + url);
|
204
|
scripts[url] = false;
|
205
|
switch (nodeName) {
|
206
|
case 'body':
|
207
|
NodeList = document.body.querySelectorAll('SCRIPT');
|
208
|
break;
|
209
|
default:
|
210
|
NodeList = document.head.querySelectorAll('SCRIPT');
|
211
|
break;
|
212
|
}
|
213
|
if (NodeList) {
|
214
|
len = NodeList.length - 1;
|
215
|
}
|
216
|
//console.info(NodeList);
|
217
|
// console.info(' JS ' + url);
|
218
|
|
219
|
try {
|
220
|
var js = document.createElement('SCRIPT');
|
221
|
js.setAttribute('type', 'text/javascript'); // optional, if not a html5 node
|
222
|
js.setAttribute('src', url); // src setzen
|
223
|
js.setAttribute('charset', 'UTF-8');
|
224
|
// js.setAttribute("async", true); // HTML5 Asyncron attribute
|
225
|
done = false;
|
226
|
if (nodeName == 'body') {
|
227
|
node = window.document.body.querySelectorAll('SCRIPT') [len];
|
228
|
node.parentNode.appendChild( js );
|
229
|
console.info( js );
|
230
|
// script.parentNode.insertBefore(js,script);
|
231
|
} else {
|
232
|
node = window.document.head.querySelectorAll('SCRIPT') [len];
|
233
|
node.parentNode.appendChild( js );
|
234
|
}
|
235
|
} catch (e) {
|
236
|
str = '<script type=\'text/javascript\' src=\'' + url + '\' charset="UTF-8"><' + '/script>';
|
237
|
document.write(str);
|
238
|
}
|
239
|
console.info( node );
|
240
|
}
|
241
|
|
242
|
// load css only within head
|
243
|
if ((typeof url !== 'undefined') && (urlExt === 'css')) {
|
244
|
//console.info(urlExt + ' = 2.) ' + url);
|
245
|
scripts[url] = false;
|
246
|
try {
|
247
|
var css = document.createElement('LINK');
|
248
|
len = 0;
|
249
|
css.setAttribute('type', 'text/css');
|
250
|
css.setAttribute('rel', 'stylesheet');
|
251
|
css.setAttribute('media', 'all');
|
252
|
css.setAttribute('href', url);
|
253
|
NodeList = window.document.querySelectorAll('LINK');
|
254
|
if (NodeList) {
|
255
|
len = NodeList.length - 1;
|
256
|
};
|
257
|
// insert after last link element if exist otherwise before first script
|
258
|
if (len > - 1) {
|
259
|
node = window.document.head.querySelectorAll('LINK') [len];
|
260
|
// console.info( len );
|
261
|
// console.info(node);
|
262
|
// return false;
|
263
|
node.parentNode.insertBefore(css, node.nextSibling);
|
264
|
// console.info('CSS ' + url);
|
265
|
} else {
|
266
|
node = window.document.head.querySelectorAll('SCRIPT') [0];
|
267
|
node.parentNode.insertBefore(css, node);
|
268
|
}
|
269
|
} catch (e) {
|
270
|
str = '<link href=\'' + url + '\' media="all" rel="stylesheet" />';
|
271
|
document.write(str);
|
272
|
}
|
273
|
}
|
274
|
// console.info( url );
|
275
|
// showMessage(url);
|
276
|
|
277
|
});
|
278
|
/**
|
279
|
*()
|
280
|
document.onreadystatechange = function () {
|
281
|
if (document.readyState == "interactive") {
|
282
|
console.info( 'Start readyState.interactive' );
|
283
|
}
|
284
|
}
|
285
|
|
286
|
// Alternativ zu load event
|
287
|
document.onreadystatechange = function () {
|
288
|
if (document.readyState == "complete") {
|
289
|
console.info( 'Start readyState.complete' );
|
290
|
}
|
291
|
}
|
292
|
|
293
|
|
294
|
window.onload = function() {
|
295
|
addEvent(document, "DOMContentLoaded", LoadOnFly);
|
296
|
console.info( 'Start window.onload' );
|
297
|
};
|
298
|
*/
|
299
|
/*
|
300
|
undefined
|
301
|
*/
|