際際滷

際際滷Share a Scribd company logo
JavaScript Dynamic Loading
JavaScript Dynamic Loading
Response Time
0

                0.1


                 1


Jakob Nielsen   10
0

                0.1


                 1


Jakob Nielsen   10
JavaScript Dynamic Loading
Huge JavaScript
91%
JavaScript Dynamic Loading
75%
JavaScript Dynamic Loading
Heavy loading   Quick response
JavaScript Dynamic Loading
JavaScript
JavaScript Dynamic Loading
JavaScript Dynamic Loading
JavaScript Dynamic Loading
<html>
 <body>
    :
  <script src=/slideshow/javascript-dynamic-loading/7693028/ ̄jQuery.js ̄></script>
  <script src= ̄jq-plugin-x.js ̄></script>
  <script src= ̄jq-plugin-y.js ̄></script>
  <script src= ̄prototype.js ̄></script>
  <script src= ̄pt-plugin-x.js ̄></script>
  <script src= ̄pt-plugin-y.js ̄></script>
  <script src= ̄init-rendering.js ̄>
  <script src= ̄facebook-plugin.js ̄>
  <script src= ̄twitter-plugin.js ̄>
 </body>
</html>
<html>
 <head>
  <script src=/slideshow/javascript-dynamic-loading/7693028/ ̄loader.js ̄></script>
  <script>
     document.onload = function() {
       /* do loading */
     };
  </script>
 </head>
</html>
How
XHR + eval

var xhrObj = new XMLHttpRequest();
xhrObj.onreadystatechange =
 function() {
   if ( xhrObj.readyState == 4 ) {
     eval(xhrObj.responseText);
   }
 };
xhrObj.open('GET', 'A.js', true);
xhrObj.send('');
XHR + injection
var xhrObj = new XMLHttpRequest();
xhrObj.onreadystatechange =
 function() {
   if ( xhrObj.readyState == 4 ) {
     var scriptElem = document.createElement('script');
     var headElem = document.getElementsByTagName('head')[0];
     headElem.appendChild(scriptElem);
     scriptElem.text = xhrObj.responseText;
   }
 };
xhrObj.open('GET', 'A.js', true);
xhrObj.send('');
dom element


var scriptElem = document.createElement('script');
scriptElem.type= 'text/javascript';
scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/&

var headElem = document.getElementsByTagName('head')[0];
headElem.appendChild(scriptElem);
dom element (not execute)


var scriptElem = document.createElement('script');
scriptElem.type= 'text/html';
scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/&

var headElem = document.getElementsByTagName('head')[0];
headElem.appendChild(scriptElem);
JavaScript Dynamic Loading
JavaScript Dynamic Loading
XHR
var xhrObj = new XMLHttpRequest();

xhrObj.onreadystatechange =
 function() {
   if ( xhrObj.readyState == 4 ) {
     /* eval or injection */

        /* JavaScript onload */
    }
  };

xhrObj.open('GET', 'A.js', true);
xhrObj.send('');
dom element


var scriptElem = document.createElement('script');
scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/&

scriptElem.onload = function() { /* JavaScript onload */ };

document.getElementsByTagName('head').appendChild(scriptElem);
with IE :)
var scriptElem = document.createElement('script');
scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/&

scriptElem.onload = function() {
  if ( !scriptElem.onloadDone ) {
    scriptElem.onloadDone = true;
    /* JavaScript onload */
  }
};
scriptElem.onreadystatechange = function() { /* for IE */
  if ( xhrObj.readyState == 4 || xhrObj.readyState == 2 ) {
    if ( !scriptElem.onloadDone ) {
      scriptElem.onloadDone = true;
      /* JavaScript onload */
    }
  }
};

document.getElementsByTagName('head').appendChild(scriptElem);
JavaScript Dynamic Loading
When / What
JavaScript Dynamic Loading
JavaScript Dynamic Loading
JavaScript Dynamic Loading
JavaScript Dynamic Loading
The three things you should know
1
JavaScript Dynamic Loading
2
JavaScript Dynamic Loading
3
JavaScript Dynamic Loading
UI
Points
JavaScript Dynamic Loading
One more thing ...
JavaScript Dynamic Loading
JavaScript Dynamic Loading
JavaScript Dynamic Loading
Thunk you
JavaScript Dynamic Loading
JavaScript Dynamic Loading

More Related Content

JavaScript Dynamic Loading

  • 4. 0 0.1 1 Jakob Nielsen 10
  • 5. 0 0.1 1 Jakob Nielsen 10
  • 8. 91%
  • 10. 75%
  • 12. Heavy loading Quick response
  • 18. <html> <body> : <script src=/slideshow/javascript-dynamic-loading/7693028/ ̄jQuery.js ̄></script> <script src= ̄jq-plugin-x.js ̄></script> <script src= ̄jq-plugin-y.js ̄></script> <script src= ̄prototype.js ̄></script> <script src= ̄pt-plugin-x.js ̄></script> <script src= ̄pt-plugin-y.js ̄></script> <script src= ̄init-rendering.js ̄> <script src= ̄facebook-plugin.js ̄> <script src= ̄twitter-plugin.js ̄> </body> </html>
  • 19. <html> <head> <script src=/slideshow/javascript-dynamic-loading/7693028/ ̄loader.js ̄></script> <script> document.onload = function() { /* do loading */ }; </script> </head> </html>
  • 20. How
  • 21. XHR + eval var xhrObj = new XMLHttpRequest(); xhrObj.onreadystatechange = function() { if ( xhrObj.readyState == 4 ) { eval(xhrObj.responseText); } }; xhrObj.open('GET', 'A.js', true); xhrObj.send('');
  • 22. XHR + injection var xhrObj = new XMLHttpRequest(); xhrObj.onreadystatechange = function() { if ( xhrObj.readyState == 4 ) { var scriptElem = document.createElement('script'); var headElem = document.getElementsByTagName('head')[0]; headElem.appendChild(scriptElem); scriptElem.text = xhrObj.responseText; } }; xhrObj.open('GET', 'A.js', true); xhrObj.send('');
  • 23. dom element var scriptElem = document.createElement('script'); scriptElem.type= 'text/javascript'; scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/& var headElem = document.getElementsByTagName('head')[0]; headElem.appendChild(scriptElem);
  • 24. dom element (not execute) var scriptElem = document.createElement('script'); scriptElem.type= 'text/html'; scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/& var headElem = document.getElementsByTagName('head')[0]; headElem.appendChild(scriptElem);
  • 27. XHR var xhrObj = new XMLHttpRequest(); xhrObj.onreadystatechange = function() { if ( xhrObj.readyState == 4 ) { /* eval or injection */ /* JavaScript onload */ } }; xhrObj.open('GET', 'A.js', true); xhrObj.send('');
  • 28. dom element var scriptElem = document.createElement('script'); scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/& scriptElem.onload = function() { /* JavaScript onload */ }; document.getElementsByTagName('head').appendChild(scriptElem);
  • 29. with IE :) var scriptElem = document.createElement('script'); scriptElem.src = /slideshow/javascript-dynamic-loading/7693028/& scriptElem.onload = function() { if ( !scriptElem.onloadDone ) { scriptElem.onloadDone = true; /* JavaScript onload */ } }; scriptElem.onreadystatechange = function() { /* for IE */ if ( xhrObj.readyState == 4 || xhrObj.readyState == 2 ) { if ( !scriptElem.onloadDone ) { scriptElem.onloadDone = true; /* JavaScript onload */ } } }; document.getElementsByTagName('head').appendChild(scriptElem);
  • 36. The three things you should know
  • 37. 1
  • 39. 2
  • 41. 3
  • 43. UI

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n