Guest User

Untitled

a guest
Jan 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. (function ($) {
  2. var defaultOptions = {};
  3. var regex = new RegExp('<link[^>]*/>');
  4. var appendCssLinksToHeader = function(content){
  5. ///<summary>
  6. /// Fetches link elements from the content and adds the link elements
  7. /// to the head element of the html document.
  8. ///</summary>
  9. var links = content.match(/<link[^>]*\/>/g);
  10. if (links){
  11. var head = $('head');
  12. $(links).each(function(){
  13. head.append(this.toString());
  14. });
  15. }
  16. };
  17.  
  18. var _loadScript = function (url, options) {
  19. ///<summary>
  20. /// Hijacking the document.write method and retrieves the script
  21. /// on the target location. If the options parameter contains a
  22. /// target element, the elements html will be replaced by the
  23. /// content written into the document.write method. If the options
  24. /// parameter contains a callback, the callback will be called once
  25. /// everything is done.
  26. ///
  27. /// Restores the original document.write once completed. Returns
  28. /// the jQuery ajax request object to allow deferring other pasties.
  29. ///</summary>
  30. var content = [];
  31. var oldWriter = document.write;
  32. document.write = function (str) {
  33. content.push(str);
  34. };
  35. var deferrer = new $.Deferred();
  36. $.getScript(url,
  37. function () {
  38. document.write = oldWriter;
  39. var joinedContent = content.join('');
  40. if (options.targetElement && $.isFunction(options.targetElement.html)){
  41. //Internet explorer doesn't load the css links when just added to the
  42. //target element, therefore we add those to the header.
  43. appendCssLinksToHeader(joinedContent);
  44. options.targetElement.html(joinedContent);
  45. }
  46. if (options.callback !== null && $.isFunction(options.callback))
  47. //Do the callback with a timeout to allow the browser to enforce css
  48. //rules on potential new html before calling the callback.
  49. setTimeout(function(){options.callback(joinedContent);},1);
  50. deferrer.resolve();
  51. }
  52. );
  53. return deferrer.promise();
  54. };
  55.  
  56. var lastScriptLoader = null;
  57. var _deferringScriptLoader = function (url, options) {
  58. ///<summary>
  59. /// Makes sure that we do not load multiple scripts at the same this. This
  60. /// is important since we hijack the document.write method.
  61. ///</summary>
  62. if (lastScriptLoader == null)
  63. lastScriptLoader = _loadScript(url, options);
  64. else {
  65. var deferrer = new $.Deferred();
  66. var scriptDeferrer = new $.Deferred();
  67. lastScriptLoader = lastScriptLoader.then(function () { deferrer.resolve();});
  68. deferrer.promise().then(function(){_loadScript(url, options).then(function(){scriptDeferrer.resolve();});});
  69. lastScriptLoader = scriptDeferrer.promise();
  70. }
  71. };
  72. var _getSettings = function(options){
  73. ///<summary>
  74. /// Loads the default settings and extends them with the user-provided
  75. /// settings or the user-provided function object.
  76. ///</summary>
  77. var settings = null;
  78. if (options && !$.isFunction(options))
  79. settings = $.extend({}, defaultOptions, options);
  80. else
  81. settings = $.extend({}, defaultOptions);
  82. if ($.isFunction(options))
  83. settings.callback = options;
  84. return settings;
  85. };
  86. var _captureWriteExternal = function (url, options) {
  87. ///<summary>
  88. /// Loads the script and enters any content written to the document.write
  89. /// method as the HTML of the elements this methods is called on (if any).
  90. /// If a callback is supplied, either as a function for options or given
  91. /// options as an object with a callback property, the callback will be
  92. /// called once the script loading is completed.
  93. ///</summary>
  94. if (this && this.length > 0){
  95. return $(this).each(function(){
  96. var settings = _getSettings(options);
  97. settings.targetElement = $(this);
  98. _deferringScriptLoader(url, settings);
  99. });
  100. }
  101. else {
  102. _deferringScriptLoader(url, _getSettings(options));
  103. }
  104. };
  105. $.fn.captureDocumentWrite = _captureWriteExternal;
  106. })(jQuery);
Add Comment
Please, Sign In to add comment