SlideShare una empresa de Scribd logo
1 de 52
JavaScript Dynamic Loading
Response Time
0

                0.1


                 1


Jakob Nielsen   10
0

                0.1


                 1


Jakob Nielsen   10
Huge JavaScript
91%
75%
Heavy loading   Quick response
JavaScript Dynamic Loading
JavaScript
<html>
 <body>
    :
  <script src=”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=”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 = 'http://anydomain.com/A.js';

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


var scriptElem = document.createElement('script');
scriptElem.type= 'text/html';
scriptElem.src = 'http://anydomain.com/A.js';

var headElem = document.getElementsByTagName('head')[0];
headElem.appendChild(scriptElem);
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 = 'http://anydomain.com/A.js';

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

document.getElementsByTagName('head').appendChild(scriptElem);
with IE :)
var scriptElem = document.createElement('script');
scriptElem.src = 'http://anydomain.com/A.js';

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);
When / What
The three things you should know
1
2
3
UI
Points
One more thing ...
Thunk you
JavaScript Dynamic Loading
JavaScript Dynamic Loading

Más contenido relacionado

La actualidad más candente

aggregation and indexing with suitable example using MongoDB.
aggregation and indexing with suitable example using MongoDB.aggregation and indexing with suitable example using MongoDB.
aggregation and indexing with suitable example using MongoDB.bhavesh lande
 
Acessardados Aula7
Acessardados Aula7Acessardados Aula7
Acessardados Aula7softeam
 
Node.js をさりげなく取り入れた 最近のフロントエンド事情について
Node.js をさりげなく取り入れた 最近のフロントエンド事情についてNode.js をさりげなく取り入れた 最近のフロントエンド事情について
Node.js をさりげなく取り入れた 最近のフロントエンド事情についてkamiyam .
 
JavaScript Assíncrono
JavaScript AssíncronoJavaScript Assíncrono
JavaScript AssíncronoNatã Barbosa
 
Collection pipeline par Mathieu Godart
Collection pipeline par  Mathieu GodartCollection pipeline par  Mathieu Godart
Collection pipeline par Mathieu GodartCocoaHeads France
 
นาย ณรงค์ฤทธิ์ ฮดฤาชา
นาย ณรงค์ฤทธิ์ ฮดฤาชานาย ณรงค์ฤทธิ์ ฮดฤาชา
นาย ณรงค์ฤทธิ์ ฮดฤาชาNarongrit Hotrucha
 
HTTP Interceptors com AngularJS
HTTP Interceptors com AngularJSHTTP Interceptors com AngularJS
HTTP Interceptors com AngularJSRodrigo Branas
 
React - podsumowanie z placu boju
React - podsumowanie z placu bojuReact - podsumowanie z placu boju
React - podsumowanie z placu bojuRadosław Mejer
 
CakePHP 事例紹介 @ogaoga
CakePHP 事例紹介 @ogaogaCakePHP 事例紹介 @ogaoga
CakePHP 事例紹介 @ogaogaTsutomu Ogasawara
 
Introduction to Service Worker
Introduction to Service WorkerIntroduction to Service Worker
Introduction to Service WorkerShogo Sensui
 
Working With Ajax Frameworks
Working With Ajax FrameworksWorking With Ajax Frameworks
Working With Ajax FrameworksJonathan Snook
 
Memanggil prosedur sendiri dari program utama 1
Memanggil prosedur sendiri dari program utama 1Memanggil prosedur sendiri dari program utama 1
Memanggil prosedur sendiri dari program utama 1Wildan Live
 

La actualidad más candente (20)

Testování prakticky
Testování praktickyTestování prakticky
Testování prakticky
 
aggregation and indexing with suitable example using MongoDB.
aggregation and indexing with suitable example using MongoDB.aggregation and indexing with suitable example using MongoDB.
aggregation and indexing with suitable example using MongoDB.
 
8. CodeIgniter move2
8. CodeIgniter move28. CodeIgniter move2
8. CodeIgniter move2
 
Phpex3
Phpex3Phpex3
Phpex3
 
Acessardados Aula7
Acessardados Aula7Acessardados Aula7
Acessardados Aula7
 
Node.js をさりげなく取り入れた 最近のフロントエンド事情について
Node.js をさりげなく取り入れた 最近のフロントエンド事情についてNode.js をさりげなく取り入れた 最近のフロントエンド事情について
Node.js をさりげなく取り入れた 最近のフロントエンド事情について
 
JavaScript Assíncrono
JavaScript AssíncronoJavaScript Assíncrono
JavaScript Assíncrono
 
Collection pipeline par Mathieu Godart
Collection pipeline par  Mathieu GodartCollection pipeline par  Mathieu Godart
Collection pipeline par Mathieu Godart
 
Clase 10 electiva profesional 3 aws rds php y mysql
Clase 10 electiva profesional 3 aws rds php y mysqlClase 10 electiva profesional 3 aws rds php y mysql
Clase 10 electiva profesional 3 aws rds php y mysql
 
นาย ณรงค์ฤทธิ์ ฮดฤาชา
นาย ณรงค์ฤทธิ์ ฮดฤาชานาย ณรงค์ฤทธิ์ ฮดฤาชา
นาย ณรงค์ฤทธิ์ ฮดฤาชา
 
Validaformulariojs
ValidaformulariojsValidaformulariojs
Validaformulariojs
 
HTTP Interceptors com AngularJS
HTTP Interceptors com AngularJSHTTP Interceptors com AngularJS
HTTP Interceptors com AngularJS
 
Zamyakin
ZamyakinZamyakin
Zamyakin
 
React - podsumowanie z placu boju
React - podsumowanie z placu bojuReact - podsumowanie z placu boju
React - podsumowanie z placu boju
 
Local storages
Local storagesLocal storages
Local storages
 
CakePHP 事例紹介 @ogaoga
CakePHP 事例紹介 @ogaogaCakePHP 事例紹介 @ogaoga
CakePHP 事例紹介 @ogaoga
 
Introduction to Service Worker
Introduction to Service WorkerIntroduction to Service Worker
Introduction to Service Worker
 
Aplicacion turbogenerador java
Aplicacion turbogenerador javaAplicacion turbogenerador java
Aplicacion turbogenerador java
 
Working With Ajax Frameworks
Working With Ajax FrameworksWorking With Ajax Frameworks
Working With Ajax Frameworks
 
Memanggil prosedur sendiri dari program utama 1
Memanggil prosedur sendiri dari program utama 1Memanggil prosedur sendiri dari program utama 1
Memanggil prosedur sendiri dari program utama 1
 

Más de Tomokazu Kiyohara

JavaScript で OS X を自動操作
JavaScript で OS X を自動操作JavaScript で OS X を自動操作
JavaScript で OS X を自動操作Tomokazu Kiyohara
 
Google Cloud Platform を支える技術 …のごく一部
Google Cloud Platform を支える技術 …のごく一部Google Cloud Platform を支える技術 …のごく一部
Google Cloud Platform を支える技術 …のごく一部Tomokazu Kiyohara
 
Web API をデバックするときに必要なたったひとつのこと
Web API をデバックするときに必要なたったひとつのことWeb API をデバックするときに必要なたったひとつのこと
Web API をデバックするときに必要なたったひとつのことTomokazu Kiyohara
 
明日から使えるコーディングツール
明日から使えるコーディングツール明日から使えるコーディングツール
明日から使えるコーディングツールTomokazu Kiyohara
 
Text-Objects - vim's elegant function
Text-Objects - vim's elegant functionText-Objects - vim's elegant function
Text-Objects - vim's elegant functionTomokazu Kiyohara
 
LiveStyle for Vim - Quick start
LiveStyle for Vim - Quick startLiveStyle for Vim - Quick start
LiveStyle for Vim - Quick startTomokazu Kiyohara
 
対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介
対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介
対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介Tomokazu Kiyohara
 
Compact Web - Remind "web compression" -
Compact Web - Remind "web compression" -Compact Web - Remind "web compression" -
Compact Web - Remind "web compression" -Tomokazu Kiyohara
 
USTREAMの視聴率を上げよう!
USTREAMの視聴率を上げよう!USTREAMの視聴率を上げよう!
USTREAMの視聴率を上げよう!Tomokazu Kiyohara
 

Más de Tomokazu Kiyohara (15)

JavaScript で OS X を自動操作
JavaScript で OS X を自動操作JavaScript で OS X を自動操作
JavaScript で OS X を自動操作
 
Google Cloud Platform を支える技術 …のごく一部
Google Cloud Platform を支える技術 …のごく一部Google Cloud Platform を支える技術 …のごく一部
Google Cloud Platform を支える技術 …のごく一部
 
イベント継続のコツ
イベント継続のコツイベント継続のコツ
イベント継続のコツ
 
Web API をデバックするときに必要なたったひとつのこと
Web API をデバックするときに必要なたったひとつのことWeb API をデバックするときに必要なたったひとつのこと
Web API をデバックするときに必要なたったひとつのこと
 
明日から使えるコーディングツール
明日から使えるコーディングツール明日から使えるコーディングツール
明日から使えるコーディングツール
 
Atom.io Quick Scripting
Atom.io Quick ScriptingAtom.io Quick Scripting
Atom.io Quick Scripting
 
Text-Objects - vim's elegant function
Text-Objects - vim's elegant functionText-Objects - vim's elegant function
Text-Objects - vim's elegant function
 
LiveStyle for Vim - Quick start
LiveStyle for Vim - Quick startLiveStyle for Vim - Quick start
LiveStyle for Vim - Quick start
 
こわくないプルリク
こわくないプルリクこわくないプルリク
こわくないプルリク
 
Github's HUB
Github's HUBGithub's HUB
Github's HUB
 
対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介
対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介
対サイバー攻撃アラートシステム “DAEDALUS”(ダイダロス)の紹介
 
Beginner's Sinatra
Beginner's SinatraBeginner's Sinatra
Beginner's Sinatra
 
Compact Web - Remind "web compression" -
Compact Web - Remind "web compression" -Compact Web - Remind "web compression" -
Compact Web - Remind "web compression" -
 
Zen coding15min
Zen coding15minZen coding15min
Zen coding15min
 
USTREAMの視聴率を上げよう!
USTREAMの視聴率を上げよう!USTREAMの視聴率を上げよう!
USTREAMの視聴率を上げよう!
 

JavaScript Dynamic Loading

  • 2.
  • 4. 0 0.1 1 Jakob Nielsen 10
  • 5. 0 0.1 1 Jakob Nielsen 10
  • 6.
  • 8. 91%
  • 9.
  • 10. 75%
  • 11.
  • 12. Heavy loading Quick response
  • 15.
  • 16.
  • 17.
  • 18. <html> <body> : <script src=”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=”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 = 'http://anydomain.com/A.js'; 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 = 'http://anydomain.com/A.js'; var headElem = document.getElementsByTagName('head')[0]; headElem.appendChild(scriptElem);
  • 25.
  • 26.
  • 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 = 'http://anydomain.com/A.js'; scriptElem.onload = function() { /* JavaScript onload */ }; document.getElementsByTagName('head').appendChild(scriptElem);
  • 29. with IE :) var scriptElem = document.createElement('script'); scriptElem.src = 'http://anydomain.com/A.js'; 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);
  • 30.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36. The three things you should know
  • 37. 1
  • 38.
  • 39. 2
  • 40.
  • 41. 3
  • 42.
  • 43. UI
  • 45.
  • 47.
  • 48.
  • 49.

Notas del editor

  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