Skip to content

Commit c79aba9

Browse files
committed
bootstrap angular on "document ready" instead of window.onload
- use jqLite api to bootstrap angular - when jQuery is present DOMContentLoaded or hacks for IE are used - when jqLite is present DOMContentLoaded is used for modern browsers and IE9 and window.onload is used for other browsers. - test html for comparing DOMContentLoaded with window.onload Closes angular#224
1 parent 84dedb8 commit c79aba9

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

perf/DCLvsWindowOnLoad.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!doctype html>
2+
<!--
3+
4+
This test demonstrates the time difference between document's DOMContentLoaded and window's load events.
5+
6+
-->
7+
<html>
8+
<head>
9+
<script>
10+
startTS = new Date().getTime();
11+
onDOMContentLoadedTS = 0; // default for browsers where DOMCL is not supported
12+
</script>
13+
<title>DOMContentLoaded test</title>
14+
<script src="../build/angular.min.js" ng:autobind></script>
15+
<script>
16+
angular.element(document).bind('DOMContentLoaded', function(e) {onDOMContentLoadedTS = new Date().getTime()});
17+
angular.element(window).bind('load', function(e) {
18+
onloadTS = new Date().getTime();
19+
log.innerHTML = 'start: ' + new Date(startTS) + '<br/>DOMContentLoaded: +' + (onDOMContentLoadedTS - startTS) + 'ms<br/> load: +' + (onloadTS - startTS) + 'ms';
20+
});
21+
</script>
22+
</head>
23+
<body>
24+
<h1>DOMContentLoaded test</h1>
25+
<p>{{ 'yay!' || 'angular starting...' }}</p>
26+
27+
<img width="100px" src="http://lh5.ggpht.com/_BLyMhylclm0/TST_bbGH0zI/AAAAAAAAATY/oNUn9kivKN8/s912/1020047.jpg" />
28+
<img width="100px" src="http://lh5.ggpht.com/_MqEybfAuUFk/TSOOiegUlPI/AAAAAAAADHY/AEwEWc64_-M/s800/IMG_7294.JPG" />
29+
<img width="100px" src="http://lh3.ggpht.com/_LdjD3ua8rpE/TSOW99rwjZI/AAAAAAAAFC0/0qJRhhN45RM/s912/Saison%2010%20%2834%29.JPG" />
30+
<img width="100px" src="http://lh6.ggpht.com/_oy_-am3CVUw/TSOQBddZpwI/AAAAAAAACaw/ogFgoD79bVE/s912/P1100886.JPG" />
31+
<img width="100px" src="http://lh4.ggpht.com/_srSaA7ZN7oc/TDdxXbA_i1I/AAAAAAAAQ2w/ii3vgrnfCrM/s800/Urlaub10%20157.jpg" />
32+
<img width="100px" src="http://lh5.ggpht.com/_y6vXu6iRrfM/SIaYhRQBYNI/AAAAAAAAAmE/lV2NYwxtsQM/s912/North%20Dakota%20Trip%20014.JPG" />
33+
<img width="100px" src="http://lh5.ggpht.com/_Jjv9cIn9cS8/RuwZCgfOl6I/AAAAAAAAAOc/QrrMe8vpawg/s800/Shark%20Trip%20-%20day%202%20513.JPG" />
34+
35+
<p id="log"></p>
36+
</body>
37+
</html>

src/angular.prefix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
(function(window, document, previousOnLoad){
24+
(function(window, document){

src/angular.suffix

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

2-
window.onload = function(){
3-
try {
4-
if (previousOnLoad) previousOnLoad();
5-
} catch(e) {}
2+
jqLite(document).ready(function(){
63
angularInit(angularJsConfig(document));
7-
};
4+
});
85

9-
})(window, document, window.onload);
6+
})(window, document);

src/jqLite.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ JQLite.prototype = {
8787
})(this[0]);
8888
},
8989

90+
ready: function(fn) {
91+
var fired = false;
92+
93+
function trigger() {
94+
if (fired) return;
95+
fired = true;
96+
fn();
97+
}
98+
99+
this.bind('DOMContentLoaded', trigger); // works for modern browsers and IE9
100+
jqLite(window).bind('load', trigger); // fallback to window.onload for others
101+
},
102+
90103
bind: function(type, fn){
91104
var self = this,
92105
element = self[0],

src/scenario/angular.prefix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
* THE SOFTWARE.
2323
*/
24-
(function(window, document, previousOnLoad){
24+
(function(window, document){
2525
var _jQuery = window.jQuery.noConflict(true);

src/scenario/angular.suffix

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
var $scenario = new angular.scenario.Runner(window);
22

3-
window.onload = function() {
4-
try {
5-
if (previousOnLoad) previousOnLoad();
6-
} catch(e) {}
3+
jqLite(document).ready(function() {
74
angularScenarioInit($scenario, angularJsConfig(document));
8-
};
5+
});
96

10-
})(window, document, window.onload);
7+
})(window, document);

0 commit comments

Comments
 (0)