日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > web前端 > htmlcss > jQuery Alternate Source in HTML

jQuery Alternate Source in HTML

來源:程序員人生   發布時間:2015-06-18 09:47:48 閱讀次數:4249次

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

<script src="http://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="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" data-alt-src="http://code.jquery.com/jquery⑵.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="http://www.jyygyx.com/uploadfile/cj/20150502/'+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="http://www.jyygyx.com/uploadfile/cj/20150502/'+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="http://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
未經允許,不準轉載

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 激情自拍视频 | 成人一级毛片 | 精品99在线 | 国产午夜精品一区二区三区四区 | 精品久久久久久久久久中文字幕 | 好吊日精品视频 | 男插女网站 | 国产精品精品视频一区二区三区 | 成人黄色电影视频 | 日本久久精品视频 | 久久久精品亚洲 | 可以免费看av的网站 | 欧洲xxx| 国产精品中文字幕在线播放 | 欧美日韩18 | 一本在线 | 福利天堂| 欧美二区视频 | 久久99精品久久久久久 | 国产高清网站 | 中文字幕高清 | 亚洲国内精品 | 国产黄色一级电影 | 精品视频999 | 自拍偷拍在线视频 | 日韩欧美在线免费观看 | 在线高潮 | 亚洲精品国产综合区久久久久久久 | 国产成人a亚洲精品 | 中文字幕国产一区 | 婷婷不卡 | 久久鬼| 日韩欧美亚洲国产精品字幕久久久 | 成人av免费在线观看 | 国产精品自拍一区 | 国产精品久久久久高潮 | 福利一区福利二区 | 成年人黄色片 | 福利在线观看 | 亚洲国内精品 | 黄色片在线看 |