Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. var element = document.getElementById('element');
  2. element.style.color = 'red';
  3.  
  4. <body>
  5. <div>...</div>
  6. <script>
  7. // Ваш скрипт
  8. </script>
  9. </body>
  10.  
  11. window.onload = function() {
  12. // Ваш скрипт
  13. };
  14.  
  15. var windowOnloadAdd = function (event) {
  16. if ( window.onload ){
  17. window.onload = window.onload + event;
  18. } else {
  19. window.onload = event;
  20. };
  21. };
  22.  
  23. windowOnloadAdd(function() {
  24. // Ваш скрипт
  25. });
  26.  
  27. function onload() {
  28. // Ваш скрипт
  29. };
  30.  
  31. <body>
  32. ...
  33. <script>
  34. onload();
  35. </script>
  36. </body>
  37.  
  38. function myFunc() {
  39. // Ваш скрипт
  40. };
  41.  
  42. <body onload="myFunc()">...</body>
  43.  
  44. document.onreadystatechange = function(){
  45. if(document.readyState === 'complete'){
  46. // Ваш скрипт
  47. }
  48. }
  49.  
  50. document.addEventListener('DOMContentLoaded', function() {
  51. // Ваш скрипт
  52. }, false);
  53.  
  54. $$r(function() {
  55. // Ваш скрипт
  56. });
  57.  
  58. $(function() {
  59. // Ваш скрипт
  60. });
  61.  
  62. $(document).ready(function() {
  63. // Ваш скрипт
  64. });
  65.  
  66. YAHOO.util.Event.onDOMReady(function(){
  67. // Ваш скрипт
  68. });
  69.  
  70. var ready = (function(){
  71.  
  72. var readyList,
  73. DOMContentLoaded,
  74. class2type = {};
  75. class2type["[object Boolean]"] = "boolean";
  76. class2type["[object Number]"] = "number";
  77. class2type["[object String]"] = "string";
  78. class2type["[object Function]"] = "function";
  79. class2type["[object Array]"] = "array";
  80. class2type["[object Date]"] = "date";
  81. class2type["[object RegExp]"] = "regexp";
  82. class2type["[object Object]"] = "object";
  83.  
  84. var ReadyObj = {
  85. // Is the DOM ready to be used? Set to true once it occurs.
  86. isReady: false,
  87. // A counter to track how many items to wait for before
  88. // the ready event fires. See #6781
  89. readyWait: 1,
  90. // Hold (or release) the ready event
  91. holdReady: function( hold ) {
  92. if ( hold ) {
  93. ReadyObj.readyWait++;
  94. } else {
  95. ReadyObj.ready( true );
  96. }
  97. },
  98. // Handle when the DOM is ready
  99. ready: function( wait ) {
  100. // Either a released hold or an DOMready/load event and not yet ready
  101. if ( (wait === true && !--ReadyObj.readyWait) || (wait !== true && !ReadyObj.isReady) ) {
  102. // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
  103. if ( !document.body ) {
  104. return setTimeout( ReadyObj.ready, 1 );
  105. }
  106.  
  107. // Remember that the DOM is ready
  108. ReadyObj.isReady = true;
  109. // If a normal DOM Ready event fired, decrement, and wait if need be
  110. if ( wait !== true && --ReadyObj.readyWait > 0 ) {
  111. return;
  112. }
  113. // If there are functions bound, to execute
  114. readyList.resolveWith( document, [ ReadyObj ] );
  115.  
  116. // Trigger any bound ready events
  117. //if ( ReadyObj.fn.trigger ) {
  118. // ReadyObj( document ).trigger( "ready" ).unbind( "ready" );
  119. //}
  120. }
  121. },
  122. bindReady: function() {
  123. if ( readyList ) {
  124. return;
  125. }
  126. readyList = ReadyObj._Deferred();
  127.  
  128. // Catch cases where $(document).ready() is called after the
  129. // browser event has already occurred.
  130. if ( document.readyState === "complete" ) {
  131. // Handle it asynchronously to allow scripts the opportunity to delay ready
  132. return setTimeout( ReadyObj.ready, 1 );
  133. }
  134.  
  135. // Mozilla, Opera and webkit nightlies currently support this event
  136. if ( document.addEventListener ) {
  137. // Use the handy event callback
  138. document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
  139. // A fallback to window.onload, that will always work
  140. window.addEventListener( "load", ReadyObj.ready, false );
  141.  
  142. // If IE event model is used
  143. } else if ( document.attachEvent ) {
  144. // ensure firing before onload,
  145. // maybe late but safe also for iframes
  146. document.attachEvent( "onreadystatechange", DOMContentLoaded );
  147.  
  148. // A fallback to window.onload, that will always work
  149. window.attachEvent( "onload", ReadyObj.ready );
  150.  
  151. // If IE and not a frame
  152. // continually check to see if the document is ready
  153. var toplevel = false;
  154.  
  155. try {
  156. toplevel = window.frameElement == null;
  157. } catch(e) {}
  158.  
  159. if ( document.documentElement.doScroll && toplevel ) {
  160. doScrollCheck();
  161. }
  162. }
  163. },
  164. _Deferred: function() {
  165. var // callbacks list
  166. callbacks = [],
  167. // stored [ context , args ]
  168. fired,
  169. // to avoid firing when already doing so
  170. firing,
  171. // flag to know if the deferred has been cancelled
  172. cancelled,
  173. // the deferred itself
  174. deferred = {
  175.  
  176. // done( f1, f2, ...)
  177. done: function() {
  178. if ( !cancelled ) {
  179. var args = arguments,
  180. i,
  181. length,
  182. elem,
  183. type,
  184. _fired;
  185. if ( fired ) {
  186. _fired = fired;
  187. fired = 0;
  188. }
  189. for ( i = 0, length = args.length; i < length; i++ ) {
  190. elem = args[ i ];
  191. type = ReadyObj.type( elem );
  192. if ( type === "array" ) {
  193. deferred.done.apply( deferred, elem );
  194. } else if ( type === "function" ) {
  195. callbacks.push( elem );
  196. }
  197. }
  198. if ( _fired ) {
  199. deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
  200. }
  201. }
  202. return this;
  203. },
  204.  
  205. // resolve with given context and args
  206. resolveWith: function( context, args ) {
  207. if ( !cancelled && !fired && !firing ) {
  208. // make sure args are available (#8421)
  209. args = args || [];
  210. firing = 1;
  211. try {
  212. while( callbacks[ 0 ] ) {
  213. callbacks.shift().apply( context, args );//shifts a callback, and applies it to document
  214. }
  215. }
  216. finally {
  217. fired = [ context, args ];
  218. firing = 0;
  219. }
  220. }
  221. return this;
  222. },
  223.  
  224. // resolve with this as context and given arguments
  225. resolve: function() {
  226. deferred.resolveWith( this, arguments );
  227. return this;
  228. },
  229.  
  230. // Has this deferred been resolved?
  231. isResolved: function() {
  232. return !!( firing || fired );
  233. },
  234.  
  235. // Cancel
  236. cancel: function() {
  237. cancelled = 1;
  238. callbacks = [];
  239. return this;
  240. }
  241. };
  242.  
  243. return deferred;
  244. },
  245. type: function( obj ) {
  246. return obj == null ?
  247. String( obj ) :
  248. class2type[ Object.prototype.toString.call(obj) ] || "object";
  249. }
  250. }
  251. // The DOM ready check for Internet Explorer
  252. function doScrollCheck() {
  253. if ( ReadyObj.isReady ) {
  254. return;
  255. }
  256.  
  257. try {
  258. // If IE is used, use the trick by Diego Perini
  259. // http://javascript.nwbox.com/IEContentLoaded/
  260. document.documentElement.doScroll("left");
  261. } catch(e) {
  262. setTimeout( doScrollCheck, 1 );
  263. return;
  264. }
  265.  
  266. // and execute any waiting functions
  267. ReadyObj.ready();
  268. }
  269. // Cleanup functions for the document ready method
  270. if ( document.addEventListener ) {
  271. DOMContentLoaded = function() {
  272. document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
  273. ReadyObj.ready();
  274. };
  275.  
  276. } else if ( document.attachEvent ) {
  277. DOMContentLoaded = function() {
  278. // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
  279. if ( document.readyState === "complete" ) {
  280. document.detachEvent( "onreadystatechange", DOMContentLoaded );
  281. ReadyObj.ready();
  282. }
  283. };
  284. }
  285. function ready( fn ) {
  286. // Attach the listeners
  287. ReadyObj.bindReady();
  288.  
  289. var type = ReadyObj.type( fn );
  290.  
  291. // Add the callback
  292. readyList.done( fn );//readyList is result of _Deferred()
  293. }
  294. return ready;
  295. })();
  296.  
  297. ready(function() {
  298. // Ваш скрипт
  299. });
  300.  
  301. setTimout(function() {
  302. // Ваш скрипт
  303. }, 3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement