简体   繁体   English

有条件地加载jQuery自定义脚本

[英]Conditionally Loading jQuery custom scripts

First of, I am using jQuery-1.8.2... 首先,我使用的是jQuery-1.8.2 ...

I was wondering how can I load custom scripts from other server and from main server as backup. 我想知道如何从其他服务器和主服务器加载自定义脚本作为备份。

Just like loading jquery or jquery tools or jquery ui... and so on. 就像加载jquery或jquery工具或jquery ui ...等等。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="<?php echo $siteURL; ?>/js/jquery-1.8.2.min.js"%3E%3C/script%3E'))</script>

I am not really sure how can I do this for custom script... For example for the link below. 我不确定如何为自定义脚本执行此操作...例如,对于以下链接。

<script src="//dvL-den.net/custom/random-script.min.js"></script>

The first code snippet works by detecting if the jQuery global object has been created. 第一个代码片段通过检测是否已创建jQuery全局对象来工作。 That's all. 就这样。 (It presumes that no other scripts are creating any global variables named 'jQuery', but that's a reasonable -- although not infallible -- presumption.) (它假设没有其他脚本正在创建任何名为'jQuery'的全局变量,但这是合理的 - 尽管不是绝对可靠的 - 推定。)

You need to do the same -- check to see if some unique global variable, object, or property in your custom script exists, and if not, assume the script hasn't been called. 您需要执行相同操作 - 检查自定义脚本中是否存在某个唯一的全局变量,对象或属性,如果不存在,则假定尚未调用该脚本。 It can be anything, even a bizarre variable like var window._this_is_a_test = true which is set solely for this purpose. 它可以是任何东西,甚至是一个奇怪的变量,如var window._this_is_a_test = true ,它仅用于此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM