Skip to content

Instantly share code, notes, and snippets.

@kiasaki
Created November 4, 2016 13:37
Show Gist options
  • Save kiasaki/72ef003f0e078061b4789a98e3d2148e to your computer and use it in GitHub Desktop.
Save kiasaki/72ef003f0e078061b4789a98e3d2148e to your computer and use it in GitHub Desktop.
/*!
*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
export function loadScript (url, async) {
return new Promise(function (resolve, reject) {
var script = document.createElement('script');
script.src = url;
if (typeof async !== 'undefined') {
script.async = async;
}
script.onerror = reject;
script.onload = resolve;
document.head.appendChild(script);
});
}
export function loadStyles (url) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.returnType = 'text';
xhr.onload = function () {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = url;
document.head.appendChild(link);
resolve();
};
xhr.onerror = reject;
xhr.open('get', url);
xhr.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment