sgutil.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /****************************************************************
  2. * *
  3. * 代码库 *
  4. * www.dmaku.com *
  5. * 努力创建完善、持续更新插件以及模板 *
  6. * *
  7. ****************************************************************/
  8. function getHeight() {
  9. return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
  10. };
  11. function getWidth() {
  12. return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
  13. };
  14. function getTop() {
  15. return window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
  16. };
  17. function getLeft() {
  18. window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft;
  19. };
  20. function getRight() {
  21. return windowPosition.left() + windowPosition.width();
  22. };
  23. /**
  24. * 获取窗体可见度高度
  25. *
  26. * @returns
  27. */
  28. function getClientHeight() {
  29. var clientHeight = 0;
  30. if (document.body.clientHeight && document.documentElement.clientHeight) {
  31. clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight
  32. : document.documentElement.clientHeight;
  33. } else {
  34. clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight
  35. : document.documentElement.clientHeight;
  36. }
  37. return clientHeight;
  38. }
  39. /**
  40. * 获取窗体可见度宽度
  41. *
  42. * @returns
  43. */
  44. function getClientWidth() {
  45. var clientWidth = 0;
  46. if (document.body.clientWidth && document.documentElement.clientWidth) {
  47. clientWidth = (document.body.clientWidth < document.documentElement.clientWidth) ? document.body.clientWidth
  48. : document.documentElement.clientWidth;
  49. } else {
  50. clientWidth = (document.body.clientWidth > document.documentElement.clientWidth) ? document.body.clientWidth
  51. : document.documentElement.clientWidth;
  52. }
  53. return clientWidth;
  54. }
  55. function getScrollHeight() {
  56. return Math.max(getClientHeight(), document.body.scrollHeight,
  57. document.documentElement.scrollHeight);
  58. }
  59. function getScrollTop() {
  60. var scrollTop = 0;
  61. if (document.documentElement && document.documentElement.scrollTop) {
  62. scrollTop = document.documentElement.scrollTop;
  63. } else if (document.body) {
  64. scrollTop = document.body.scrollTop;
  65. }
  66. return scrollTop;
  67. }
  68. /* 获取文件后缀 */
  69. function tm_getExt(fileName) {
  70. if (fileName.lastIndexOf(".") == -1)
  71. return fileName;
  72. var pos = fileName.lastIndexOf(".") + 1;
  73. return fileName.substring(pos, fileName.length).toLowerCase();
  74. }
  75. /* 获取文件名称 */
  76. function tm_getFileName(fileName) {
  77. var pos = fileName.lastIndexOf(".");
  78. if (pos == -1) {
  79. return fileName;
  80. } else {
  81. return fileName.substring(pos,fileName.length);
  82. }
  83. }
  84. /**
  85. * 判断非空
  86. *
  87. * @param val
  88. * @returns {Boolean}
  89. */
  90. function isEmpty(val) {
  91. val = $.trim(val);
  92. if (val == null)
  93. return true;
  94. if (val == undefined || val == 'undefined')
  95. return true;
  96. if (val == "")
  97. return true;
  98. if (val.length == 0)
  99. return true;
  100. if (!/[^(^\s*)|(\s*$)]/.test(val))
  101. return true;
  102. return false;
  103. }
  104. function isNotEmpty(val) {
  105. return !isEmpty(val);
  106. }
  107. //trim() , ltrim() , rtrim()
  108. String.prototype.trim = function(){
  109. return this.replace(/(^\s*)|(\s*$)/g, "");
  110. }
  111. String.prototype.ltrim = function(){
  112. return this.replace(/(^\s*)/g, "");
  113. }
  114. String.prototype.rtrim = function() {
  115. return this.replace(/(\s*$)/g, "");
  116. }
  117. /* 刷新当前 */
  118. function tm_refreash() {
  119. window.location.href = window.location.href;
  120. }
  121. /** ******************json*************** */
  122. function jsonToString(obj) {
  123. var THIS = this;
  124. switch (typeof (obj)) {
  125. case 'string':
  126. return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';
  127. case 'array':
  128. return '[' + obj.map(THIS.jsonToString).join(',') + ']';
  129. case 'object':
  130. if (obj instanceof Array) {
  131. var strArr = [];
  132. var len = obj.length;
  133. for (var i = 0; i < len; i++) {
  134. strArr.push(THIS.jsonToString(obj[i]));
  135. }
  136. return '[' + strArr.join(',') + ']';
  137. } else if (obj == null) {
  138. return 'null';
  139. } else {
  140. var string = [];
  141. for ( var property in obj)
  142. string.push(THIS.jsonToString(property) + ':'
  143. + THIS.jsonToString(obj[property]));
  144. return '{' + string.join(',') + '}';
  145. }
  146. case 'number':
  147. return obj;
  148. case false:
  149. return obj;
  150. }
  151. }
  152. /* loading快速加载方法 */
  153. function tmLoading(content, timeout, overlay) {
  154. $.tmLoading(content, {
  155. timer : timeout,
  156. skin : "black",
  157. overlay : overlay
  158. });
  159. };
  160. /* 获取浏览器的版本 */
  161. function tm_getBroswerVersion() {
  162. var Sys = {};
  163. var ua = navigator.userAgent.toLowerCase();
  164. if (ua) {
  165. window.ActiveXObject ? Sys.version = "ie_"
  166. + ua.match(/msie ([\d]+)/)[1]
  167. : document.getBoxObjectFor ? Sys.version = "firefox_"
  168. + ua.match(/firefox\/([\d.]+)/)[1]
  169. : window.MessageEvent && !document.getBoxObjectFor ? Sys.version = "chrome"
  170. : window.opera ? Sys.version = "opera_"
  171. + ua.match(/opera.([\d.]+)/)[1]
  172. : window.openDatabase ? Sys.version = ua
  173. .match(/version\/([\d.]+)/)[1]
  174. : 0;
  175. }
  176. return Sys;
  177. }
  178. /* 判断一个元素释放包含在数组中。 */
  179. Array.prototype.contains = function(obj) {
  180. var i = this.length;
  181. while (i--) {
  182. if (this[i] === obj) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. };
  188. function getTimeFormat(startTime) {
  189. var startTimeMills = startTime.getTime();
  190. var endTimeMills = new Date().getTime();
  191. var diff = parseInt((endTimeMills - startTimeMills) / 1000);//秒
  192. var day_diff = parseInt(Math.floor(diff / 86400));//天
  193. var buffer = Array();
  194. if (day_diff < 0) {
  195. return "[error],时间越界...";
  196. } else {
  197. if (day_diff == 0 && diff < 60) {
  198. if (diff <= 0)
  199. diff = 1;
  200. buffer.push(diff + "秒前");
  201. } else if (day_diff == 0 && diff < 120) {
  202. buffer.push("1 分钟前");
  203. } else if (day_diff == 0 && diff < 3600) {
  204. buffer.push(Math.round(Math.floor(diff / 60)) + "分钟前");
  205. } else if (day_diff == 0 && diff < 7200) {
  206. buffer.push("1小时前");
  207. } else if (day_diff == 0 && diff < 86400) {
  208. buffer.push(Math.round(Math.floor(diff / 3600)) + "小时前");
  209. } else if (day_diff == 1) {
  210. buffer.push("1天前");
  211. } else if (day_diff < 7) {
  212. buffer.push(day_diff + "天前");
  213. } else if (day_diff < 30) {
  214. buffer.push(Math.round(Math.floor(day_diff / 7)) + " 星期前");
  215. } else if (day_diff >= 30 && day_diff <= 179) {
  216. buffer.push(Math.round(Math.floor(day_diff / 30)) + "月前");
  217. } else if (day_diff >= 180 && day_diff < 365) {
  218. buffer.push("半年前");
  219. } else if (day_diff >= 365) {
  220. buffer.push(Math.round(Math.floor(day_diff / 30 / 12)) + "年前");
  221. }
  222. }
  223. return buffer.toString();
  224. }
  225. /**flash版本号*/
  226. function exmayFlashVersion() {
  227. var f = "", n = navigator;
  228. if (n.plugins && n.plugins.length) {
  229. for ( var ii = 0; ii < n.plugins.length; ii++) {
  230. if (n.plugins[ii].name.indexOf('Shockwave Flash') != -1) {
  231. f = n.plugins[ii].description.split('Shockwave Flash ')[1];
  232. break;
  233. }
  234. }
  235. } else if (window.ActiveXObject) {
  236. for ( var ii = 10; ii >= 2; ii--) {
  237. try {
  238. var fl = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash."
  239. + ii + "');");
  240. if (fl) {
  241. f = ii + '.0';
  242. break;
  243. }
  244. } catch (e) {
  245. }
  246. }
  247. }
  248. return f;
  249. };
  250. function tp_getBrowse() {
  251. var sUA=navigator.userAgent;
  252. //检测IE浏览器
  253. if ((navigator.appName == "Microsoft Internet Explorer")) {
  254. //检测模拟IE浏览的OPERA。edit at 2006-11-08(ver 0.1.2)
  255. if (sUA.indexOf('Opera')!=-1) {
  256. this.browseKernel='Presto';
  257. if(window.opera && document.childNodes ) {
  258. return 'Opera 7+';
  259. } else {
  260. return 'Opera 6-';
  261. }
  262. }
  263. this.browseKernel='Trident';
  264. if(sUA.indexOf('Maxthon')!=-1) {
  265. return 'Maxthon';
  266. }
  267. if(sUA.indexOf('TencentTraveler')!=-1) { //ver 0.1.3
  268. return '腾迅TT';
  269. }
  270. if(document.getElementById) {
  271. return "IE5+";
  272. } else {
  273. return "IE4-";
  274. }
  275. }
  276. //检测Gecko浏览器
  277. if(sUA.indexOf('Gecko')!=-1) {
  278. this.browseKernel='Gecko';
  279. if(navigator.vendor=="Mozilla") {return "Mozilla";}
  280. if(navigator.vendor=="Firebird") {return "Firebird"; }
  281. if (navigator.vendor.indexOf('Google')!=-1 || sUA.indexOf('Google')!=-1) {return 'Google'; }
  282. if (sUA.indexOf('Firefox')!=-1) {return 'Firefox'; }
  283. return "Gecko";
  284. }
  285. //Netscape浏览器
  286. if(sUA.indexOf('Netscape')!=-1) {
  287. this.browseKernel='Gecko';
  288. if(document.getElementById) {
  289. return "Netscape 6+";
  290. } else {
  291. return 'Netscape 5-';
  292. }
  293. }
  294. //检测Safari浏览器
  295. if(sUA.indexOf('Safari') != -1) {this.browseKernel='KHTML';return 'Safari';}
  296. if(sUA.indexOf('konqueror')!=-1) {this.browseKernel='KHTML';return 'Konqueror';}
  297. //目前世界公认浏览网页速度最快的浏览器,但它占用的系统资源也很大。
  298. if(sUA.indexOf('Opera') != -1) {
  299. this.browseKernel='Presto';
  300. if(window.opera && document.childNodes ) {
  301. return 'Opera 7+';
  302. } else {
  303. return 'Opera 6-';
  304. }
  305. return 'Opera';
  306. }
  307. if((sUA.indexOf( 'hotjava' )!=-1) && typeof( navigator.accentColorName ) == 'undefined' ) {return 'HotJava';}
  308. if( document.all && document.getElementById && navigator.savePreferences && (sUA.indexOf( 'netfront' ) < 0 ) && navigator.appName != 'Blazer' ) {return 'Escape 5'; }
  309. //Konqueror / Safari / OmniWeb 4.5+
  310. if( navigator.vendor == 'KDE' || ( document.childNodes && ( !document.all || navigator.accentColorName ) && !navigator.taintEnabled ) ) {this.browseKernel='KHTML';return 'KDE';}
  311. if( navigator.__ice_version ) { return 'ICEbrowser';}
  312. if( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) {
  313. if( document.createElement ) {
  314. return 'iCab 3+';
  315. } else {
  316. return 'iCab 2-';
  317. }
  318. }
  319. if(document.layers && !document.classes ) {return 'Omniweb 4.2-';}
  320. if(document.layers && !navigator.mimeTypes['*'] ) {return 'Escape 4';}
  321. if(navigator.appName.indexOf( 'WebTV' ) + 1 ) {return 'WebTV';}
  322. if(sUA.indexOf( 'netgem' )!=-1 ) {return 'Netgem NetBox';}
  323. if(sUA.indexOf( 'opentv' )!=-1 ) {return 'OpenTV';}
  324. if(sUA.indexOf( 'ipanel' )!=-1) {return 'iPanel MicroBrowser';}
  325. if(document.getElementById && !document.childNodes) {return 'Clue browser';}
  326. if(document.getElementById && ( (sUA.indexOf( 'netfront' ) !=-1) || navigator.appName == 'Blazer' ) ) {return 'NetFront 3+';}
  327. if((sUA.indexOf( 'msie' ) + 1 ) && window.ActiveXObject ) {return 'Pocket Internet Explorer'; }
  328. return "Unknown";
  329. }
  330. /*字符串转日期格式,strDate要转为日期格式的字符串*/
  331. function getDate(strDate){
  332. var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
  333. function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
  334. return date;
  335. }