I have a pretty simple question that I had in my mind today. I'm linking to jquery locally, from my pc in case I don't have internet connection but I was wondering if there is a way to check if there is internet connection and if so, load jquery from CDN. Any suggestions?
Asked
Active
Viewed 1,821 times
1
-
why don't you check `HTTP Status code` and load file accordingly? – Rohit Jul 07 '16 at 08:12
-
[A similar question](http://stackoverflow.com/a/12474964/6551577) was answered a few years ago. Please take a look at it. – Alex M Jul 07 '16 at 08:20
-
may be try an ajax after load with some timeout? If the particular url does not load after specified amount of time, then include the local js. – Gogol Jul 07 '16 at 08:20
2 Answers
0
You can do this CDN fallback:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/libs/jquery-1.11.1.min.js"><\/script>')</script>
HTML5 boilerplate is using this technique: https://github.com/h5bp/html5-boilerplate/blob/master/dist/index.html
Please check this article: http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx

SandroMarques
- 6,070
- 1
- 41
- 46
0
Pure html solution:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Test CDN fallback</title>
</head>
<body>
<object data="https://cdn.invented.com/something" type="text/javascript">
<script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
</object>
<!-- in your case -->
<!-- <object data="https://code.jquery.com/jquery-3.0.0.min.js" type="text/javascript">
<script src="/local/jquery-3.0.0.min.js"></script>
</object> -->
</body>
</html>

SilentTremor
- 4,747
- 2
- 21
- 34