common.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // window.devicePixelRatio;
  2. var ajax = function(options) {
  3. var data = null;
  4. this.options = {
  5. url: "",
  6. data: {},
  7. format: "json",
  8. method: "GET",
  9. before: null,
  10. callback: null,
  11. };
  12. for (var i in options) {
  13. this.options[i] = options[i];
  14. }
  15. try {
  16. this.req = new XMLHttpRequest();
  17. } catch(e) {
  18. try {
  19. this.req = new ActiveXObject("Microsoft.XMLHTTP");
  20. } catch(e) {
  21. return false;
  22. }
  23. }
  24. this.headers = {
  25. "X-Requested-With": "XMLHTTPRequest",
  26. "Accept": 'text/javascript, text/html, application/xml, text/xml, */*'
  27. };
  28. var self = this;
  29. this.req.onreadystatechange = function() {
  30. if (self.req.readyState == 4 && self.req.status == 200) {
  31. if (self.options.format == "json") {
  32. if (this.responseText) {
  33. var result = JSON.parse(this.responseText);
  34. }
  35. } else {
  36. var result = this.responseText;
  37. }
  38. if (self.options.callback) {
  39. return self.options.callback(result);
  40. }
  41. }
  42. }
  43. var p = [];
  44. for (var i in this.options.data) {
  45. var v = this.options.data[i];
  46. if (typeof v == typeof {}) {
  47. v = JSON.stringify(v);
  48. }
  49. p.push(i + "=" + encodeURIComponent(v));
  50. }
  51. if (p.length > 0) {
  52. if (this.options.method == "GET") {
  53. var c = this.options.url.indexOf("?") != -1 ? "&" : "?";
  54. this.options.url = this.options.url + c + p.join("&");
  55. }
  56. if (this.options.method == "POST") {
  57. data = p.join("&");
  58. }
  59. }
  60. this.req.open(this.options.method, this.options.url, true);
  61. for(var i in this.headers) {
  62. try {
  63. this.req.setRequestHeader(i, this.headers[i]);
  64. } catch(e) {
  65. }
  66. }
  67. if (this.options.method == "POST") {
  68. this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  69. }
  70. this.req.send(data);
  71. if (this.options.before) {
  72. this.options.before();
  73. }
  74. }
  75. if (typeof $ == "undefined") {
  76. function $(id) {
  77. return document.getElementById(id);
  78. }
  79. }
  80. Element.prototype.nextElement = function() {
  81. var n = this;
  82. do {
  83. n = n.nextSibling;
  84. } while (n && n.nodeType != 1);
  85. return n;
  86. }
  87. Element.prototype.prevElement = function() {
  88. var n = this;
  89. do {
  90. n = n.previousSibling;
  91. } while (n && n.nodeType != 1);
  92. return n;
  93. }
  94. Element.prototype.hasClass = function(c) {
  95. var p = this.className.indexOf(c);
  96. return p != -1 && (this.className.charCodeAt(p - 1) || 32 == 32 && this.className.charCodeAt(p + c.length) || 32 == 32);
  97. }
  98. Element.prototype.addClass = function(c) {
  99. if (!this.hasClass(c)) {
  100. this.className += " " + c;
  101. }
  102. return this;
  103. }
  104. Element.prototype.removeClass = function(c) {
  105. var cls = this.className.split(" ");
  106. for (var i = 0;i < cls.length;i++) {
  107. if (cls[i] == c) {
  108. delete cls[i];
  109. }
  110. }
  111. this.className = cls.join(" ");
  112. }
  113. Element.prototype.toggleClass = function(c) {
  114. if (!this.hasClass(c)) {
  115. this.className += " " + c;
  116. } else {
  117. this.removeClass(c);
  118. }
  119. return this;
  120. }
  121. function query_get(name){
  122. var urlt = window.location.href.split("?");
  123. if(typeof(urlt[1]) != "undefined"){
  124. var gets = urlt[1].split("&");
  125. for(var i=0;i<gets.length;i++){
  126. var get = gets[i].split("=");
  127. if(get[0] == name){
  128. var value = get[1];
  129. break;
  130. }
  131. }
  132. return value;
  133. }
  134. }
  135. function getToday() {
  136. var d = new Date();
  137. var date = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
  138. return date;
  139. }
  140. function getTime() {
  141. var d = new Date();
  142. var time = d.getHours() + ':' + d.getMinutes();
  143. return time;
  144. }
  145. function switch_tab(tab) {
  146. var tabs = ['com', 'new', 'hot'];
  147. for (var i in tabs) {
  148. var el = tabs[i];
  149. $("d_" + el).style.display = "none";
  150. $(el).removeClass("selected");
  151. if (el == tab) {
  152. $("d_" + el).style.display = "";
  153. $(el).addClass("selected");
  154. }
  155. }
  156. }