jQuery Alternate Source in HTML

For better site performance, we may use popular libraries from CDN like Google Hosted Libraries.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

That‘s good. But sometimes, a storm may arise from a clear sky, the script may not be accessible. To save against a rainy day, an alternative source should be ready.

<script name="jQuery" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" data-alt-src="//code.jquery.com/jquery-2.1.3.min.js"></script>

and with the following snippet, got it.

<script title="script-alt 1.0">
if(!window.jQuery){
    document.write(‘<script name="jQueryAlt" src="‘+document.scripts.namedItem("jQuery").getAttribute("data-alt-src")+‘"><‘+‘/script>‘);
}
</script>

Note that if you want to use jQuery API directly, use document.write() rather than appendChild call like document.body.appendChild() to add a script element to document. for the appendChild call, its related loading is asynchronous in Chrome.

To replace the error script element with alternative script element when jQuery loading failed, this works:

<script title="script-alt 1.1">
(function(){
    if(!window.jQuery){
        var script=document.scripts.namedItem("jQuery");
        document.write(‘<script name="jQueryAlt" src="‘+script.getAttribute("data-alt-src")+‘"><‘+‘/script>‘);
        script.parentElement.replaceChild(document.scripts.namedItem("jQueryAlt"),script);
    }
}());
</script>

To let the alternative script element not be tagged with "jQueryAlt", one solution:

<script title="script-alt 1.2">
(function(){
    if(!window.jQuery){
        var script=document.scripts.namedItem("jQuery");
        script.src=script.getAttribute("data-alt-src");
        document.write(script.outerHTML);
        var lastItem=function(){return this.item(this.length-1);};
        script.parentElement.replaceChild(lastItem.call(document.scripts),script);
    }
}());
</script>

To make it modular, we got

<script title="script-alt 1.3">
(function(){
    function lastItem(){
        return this.item(this.length-1);
    }
    function setSrc(src){
        this.src=src;
        if(document.readyState=="complete"){this.outerHTML=this.outerHTML;return;}
        document.write(script.outerHTML);
        this.parentElement.replaceChild(lastItem.call(document.scripts),script);
    }
    if(!window.jQuery){
        var script=document.scripts.namedItem("jQuery");
        setSrc.call(script,script.getAttribute("data-alt-src"));
    }
}());
</script>

If you have your own host with SSL support, and its resources are located at the the same path as these in Google Hosted Libraries, then the only difference between your own host and Google Hosted Libraries is host. In these circumstances, you just need to specify an alternative source host.

<script name="jQuery" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" data-alt-host="ajax.myapis.com"></script>

and with the following script, alternative source may work.

<script title="script-alt 2.0">
(function(){
    function DocumentBasedURL(url){
        var a=document.createElement("a");
        a.href=url;
        return a;
    }
    function lastItem(){
        return this.item(this.length-1);
    }
    function setSrc(src){
        this.src=src;
        if(document.readyState=="complete"){this.outerHTML=this.outerHTML;return;}
        document.write(script.outerHTML);
        this.parentElement.replaceChild(lastItem.call(document.scripts),script);
    }
    if(!window.jQuery){
        var script=document.scripts.namedItem("jQuery");
        var url=new DocumentBasedURL(script.src);
        url.host=script.getAttribute("data-alt-host");
        setSrc.call(script,url.href);
    }
}());
</script>

Finally, jQuery API can be called to do what you want.

<script>
jQuery(function($){
    console.log("jQuery "+$.fn.jquery+" loaded");
});
</script>

本文来自http://blog.csdn.net/flashdelover/article/details/45421109

未经允许,不准转载

时间: 2024-11-09 04:04:03

jQuery Alternate Source in HTML的相关文章

jQuery整理笔记七----几个经典表单应用

1.文本框获得(失去)焦点 当文本框获得输入焦点时,将该文本框高亮显示,算不得一个应用,仅仅是一个小技巧,能够提高用户体验. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/

jQuery几个经典表单应用整理回顾

1.文本框获得(失去)焦点 当文本框获得输入焦点时,将该文本框高亮显示,算不得一个应用,只是一个小技巧,可以提高用户体验. [html] view plaincopy <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xht

对jQuery.extend()方法的分析

jQuery.extend方法是我们常用的方法,也是jQuery源码中的基础方法.它的主要作用是:将一个或多个“源对象”合并到一个“目标对象”中,并返回目标对象.它主要有三种表现形式: a.jQuery.extend(destination, source1, source2, source3 ....) b.jQuery.extend( source ) c.jQuery.extend(boolean, destination, source1, source2, source3 ....)

jQuery几个经典表单应用整理回想

1.文本框获得(失去)焦点 当文本框获得输入焦点时,将该文本框高亮显示,算不得一个应用.仅仅是一个小技巧,能够提高用户体验. [html] view plaincopy <span style="font-family:SimSun;font-size:12px;"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xh

Octopress博客搭建

1.Git:版本管理工具,将代码托管到GitHub 2.Ruby + DevKit:生成静态网页 3.MarkdownPad:Windows下Markdown语法编辑器 注册GitHub帐号 https://git-scm.com/downloads Git-2.11.1-64-bit.exe http://rubyinstaller.org/downloads/ DevKit-mingw64-64-4.7.2-20130224-1432-sfx.exe http://rubyinstaller

SCCM 2012 R2安装部署过程和问题(一)

在进行Windows Server 2012 R2虚拟化测试前,由于需要安装,部署和管理很多的服务器,自然会想到该如何提高效率和有效的管理.在Windows Server 2008的时代微软已经提供称作Windows部署服务(Windows Deloyment Services, WDS)的服务器角色,使用该服务器角色可以完成PXE和多播安装和部署服务器的任务.但是微软还有一个更强大的工具称为System Center Configuration Manager(SCCM).SCCM其实是Sys

artDialog4.1.7 摘自网络

jquery.artDialog.source.js学习 1 关键的对象关系 art = jQuery = $ function artDialog() {...} artDialog.fn = artDialog.prototype = artDialog.fn._init.prototype jQuery.fn = jQuery.prototype = jQuery.fn.init.prototype jQuery.extend = jQuery.fn.extend $.fn.dialog

ubuntu上利用checkinstall制作deb或rpm包

1.安装checkinstall sudo apt-get install checkinstall 2.编译源码 通常的源码编译需要经过configure或cmake,之后再通过make编译. configure or cmake make 3.打包 make后执行 sudo checkinstall 随后跟随系统提示操作: 第一步,是否生成文档,选"yes" The package documentation directory ./doc-pak does not exist.

在Windows Server 2012 中安装 .NET 3.5 Framework

问题 如今,仍然有许多程序和应用需要依靠.NET 3.5 framework 来运行.在Windows Server 2012中,微软提供了.NET 3.5 和.NET 4.5的安装选项以为你的应用程序提供支持.但在安装.NET 3.5 组件时,你要多注意一些细节. 当使用服务器管理器来安装.NET 3.5 时你可能会遇到下面的提示信息,指出源文件无法找到: 即使使用PowerShell来安装,你也可能看到类似的提示信息"The source files could not be downloa