js.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * ESL (Enterprise Standard Loader)
  3. * Copyright 2013 Baidu Inc. All rights reserved.
  4. *
  5. * @file JS Loader-Plugin
  6. * @author errorrik(errorrik@gmail.com)
  7. */
  8. // 构建环境暂不支持分析,为了能合并该plugin到loader里,
  9. // 只能暂时使用具名id
  10. define( 'js', {
  11. load: function ( resourceId, req, load, config ) {
  12. function onload() {
  13. var readyState = script.readyState;
  14. if (
  15. typeof readyState == 'undefined'
  16. || /^(loaded|complete)$/.test( readyState )
  17. ) {
  18. script.onload = script.onreadystatechange = null;
  19. script = null;
  20. load( true );
  21. }
  22. }
  23. var script = document.createElement( 'script' );
  24. script.src = req.toUrl( resourceId );
  25. script.async = true;
  26. if ( script.readyState ) {
  27. script.onreadystatechange = onload;
  28. }
  29. else {
  30. script.onload = onload;
  31. }
  32. var parent = document.getElementsByTagName( 'head' )[ 0 ]
  33. || document.body;
  34. parent.appendChild( script ) && ( parent = null );
  35. }
  36. } );