2

I have a widget which is added to random websites.

Whenever these sites have jQuery, I want my code to use it and not upload jQuery again.

How can I make by a one time setting, my function calls to jQuery call the site's jQuery, possibly with a different jQuery prefix ($)?

4

1 回答 1

0

使用匿名函数来测试 jQuery 对象是否存在。如果未定义,请将其添加到 DOM 并稍等片刻,然后再试一次:

void(function()
  {
  if (/function/.test(this["jQuery"]) == false)
    {
    var anon = arguments.callee;
    var jqs = document.createElement("script");
    jqs.src = "http://code.jquery.com/jquery-latest.js";
    document.getElementsByTagName("body")[0].appendChild(jqs);

    window.setTimeout(function () {anon();}, 500);
    }
    else
      {
      $(document).ready(function(){document.title = "jQuery"});
      }
  }
());
于 2012-02-21T23:56:42.193 回答