Skip to content

Updated execute() function #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Where:
A callback for when all files have loaded successfully.

#### error: function(Object BootUp)
A callback for when at least one file has failed.
A callback for when at least one file has failed.

#### loaded: function(Object BootUp, Number downloadedCount, Number fileCount, String path, String data)
This callback will fired after a file has successfully downloaded.
Expand Down Expand Up @@ -48,9 +48,9 @@ Get the contents of the file with the specified file name.

The simplest way to use BootUp is to just specify an array of files to download.

new BootUp(["jquery.js", "backbone.js", "site.js"]);
new BootUp(["style.css", jquery.js", "backbone.js", "site.js"]);

This will load in jQuery, Backbone and your site code (in the order that they are specified) and load them into `localStorage` (if available). On the next visit, it will just load them from `localStorage` directly.
This will load in your site CSS, jQuery, Backbone and your site code (in the order that they are specified) and load them into `localStorage` (if available). On the next visit, it will just load them from `localStorage` directly.

### Callbacks

Expand All @@ -59,7 +59,7 @@ There are three callbacks that you can use and are specified in the `options` ob
`success` is called when all the files specified in the array have been downloaded. It works similar to the `window.onload` event handler.

new BootUp(
["jquery.js", "backbone.js", "site.js"],
["style.css", "jquery.js", "backbone.js", "site.js"],
{
success: function() {
init();
Expand All @@ -71,7 +71,7 @@ There are three callbacks that you can use and are specified in the `options` ob
`error` is called if there is an issue downloading any of the files. Note, this is not called if `localStorage` is unavailable, or has become corrupt (these are handled silently by BootUp and in most cases would act like nothing happened).

new BootUp(
["jquery.js", "backbone.js", "site.js"],
["style.css", "jquery.js", "backbone.js", "site.js"],
{
error: function() {
alert("There was an error loading the files. Please try again later.");
Expand All @@ -84,7 +84,7 @@ There are three callbacks that you can use and are specified in the `options` ob
<div id="Progress"></div>
<script>
new BootUp(
["jquery.js", "backbone.js", "site.js"],
["style.css", "jquery.js", "backbone.js", "site.js"],
{
loaded: function(obj, current, maximum) {
document.getElementById("Progress").innerText = current + " of " + maximum + " downloaded...";
Expand Down
38 changes: 31 additions & 7 deletions bootup.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ var BootUp = function (files, options) {
function init() {
loadOptions(options);
fileCount = files.length;
if (loadFresh && hasStorage && localStorage.getItem("cache")) {
localStorage.removeItem("cache");
}
try {
if (hasStorage && localStorage.getItem("cache")) {
storedCache = JSON.parse(localStorage.getItem("cache"));
Expand Down Expand Up @@ -163,18 +166,39 @@ var BootUp = function (files, options) {
}

/**
* Injects a JS file into the page.
* Injects a JS or CSS file into the page.
* @private
* @param loaded the loaded data object.
*/
function execute(loaded) {
if (loaded.path.indexOf(".js") === -1) {
return;
var types = "css|js",
re = new RegExp("^"+ types, "i"),
extension = loaded.path.split("."),
filetype;

extension = extension[extension.length - 1];
filetype = re.exec(extension)[0];

switch (filetype) {

case "js":

var script = document.createElement("script");
script.type = "text/javascript";
script.text = loaded.data;
document.body.appendChild(script);

break;

case "css":

var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = loaded.data;
document.head.appendChild(style);

break;
}
var script = document.createElement("script");
script.type = "text/javascript";
script.text = loaded.data;
document.body.appendChild(script);
}

/**
Expand Down