Skip to content
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

How to handle local fallbacks? #16

Closed
tauren opened this issue Jun 22, 2011 · 3 comments
Closed

How to handle local fallbacks? #16

tauren opened this issue Jun 22, 2011 · 3 comments

Comments

@tauren
Copy link

tauren commented Jun 22, 2011

I am currently loading resources like jquery from google's CDN, but I have a fallback mechanism in place in case the CDN is unavailable:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/libs/jquery-1.6.1.min.js">\x3C/script>')</script>

What would be the best way to accomplish this using $script? How is an action taken if a resource does not get loaded? Or what if the jquery.min.js file does get loaded, but for some reason it is empty or corrupt and doesn't define window.jQuery?

@ded
Copy link
Owner

ded commented Jun 23, 2011

function ready(fn) {
  $script('//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js', function () {
    window.jQuery ? fn() : $script('js/libs/jquery-1.6.1.min.js', fn)
  })
}

@ded ded closed this as completed Jun 23, 2011
@tauren
Copy link
Author

tauren commented Jun 24, 2011

@ded - thanks for your answer, but could you explain a few things to me?

  1. What is the reason you wrapped it in the ready(fn) function? How is ready(fn) used?
  2. I assume fn would contain the code to execute once jquery is loaded. Is that correct?
  3. Doesn't $script take up to 3 params? The first is the resource/resources, the second is a function to run when all resources are loaded, and the third is a function to run if some resources didn't load. Your code just calls the second parameter, but how does that get called if the script isn't loaded from googleapis? It seems like the third parameter should be used as well.

@ded
Copy link
Owner

ded commented Jun 24, 2011

$script takes 3 args. the 2nd being an optional name. if the 2nd is used, then you have another chance to pass in a function as the 3rd.

$script.ready takes 3 args as well. the third being "which deps were not found" (eg: had no name).

regardless of each of those, the $script callback will return if it's found, or errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants