net.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * based on code from:
  3. *
  4. * @license RequireJS text 0.25.0 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
  5. * Available via the MIT or new BSD license.
  6. * see: http://github.com/jrburke/requirejs for details
  7. */
  8. define(function(require, exports, module) {
  9. "use strict";
  10. var dom = require("./dom");
  11. exports.get = function (url, callback) {
  12. var xhr = new XMLHttpRequest();
  13. xhr.open('GET', url, true);
  14. xhr.onreadystatechange = function () {
  15. //Do not explicitly handle errors, those should be
  16. //visible via console output in the browser.
  17. if (xhr.readyState === 4) {
  18. callback(xhr.responseText);
  19. }
  20. };
  21. xhr.send(null);
  22. };
  23. exports.loadScript = function(path, callback) {
  24. var head = dom.getDocumentHead();
  25. var s = document.createElement('script');
  26. s.src = path;
  27. head.appendChild(s);
  28. s.onload = s.onreadystatechange = function(_, isAbort) {
  29. if (isAbort || !s.readyState || s.readyState == "loaded" || s.readyState == "complete") {
  30. s = s.onload = s.onreadystatechange = null;
  31. if (!isAbort)
  32. callback();
  33. }
  34. };
  35. };
  36. /*
  37. * Convert a url into a fully qualified absolute URL
  38. * This function does not work in IE6
  39. */
  40. exports.qualifyURL = function(url) {
  41. var a = document.createElement('a');
  42. a.href = url;
  43. return a.href;
  44. }
  45. });