小部件中的jQuery [英] jQuery in widget

查看:64
本文介绍了小部件中的jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在可添加到随机网站的JS库添加中使用jQuery.这些网站可能已经或可能不会使用jQuery.

I want to use jQuery in an add on JS library that can be added to random websites. These websites may or may not use jQuery already.

实际上,我有3个问题:

I have 3 questions around this actually:

  1. 我可能会从自己的js脚本(而不是从文档头中的script标签)动态加载jQuery. jQuery会以这种方式工作吗?我如何确保它可以在主文档中没有标准$(document).ready(function(){}的情况下及时运行?

  1. I will probably load jQuery dynamically from my own js script (not from a script tag in the document head). Will jquery work this way? how can I make sure it will run in time without having the standard $(document).ready(function(){} in the main document?

我应该怎么做才能避免与网站代码中的现有jQuery(如果有)发生冲突.

What should I do to avoid conflicts with existing jQuery (if any) in the web site code.

是否有推荐的方法向随机网站添加包含jQuery的小部件,同时提供最少的代码和最简单的集成.

Is there a recommended way to add a widget that includes jQuery to random websites while providing minimal code and simplest integration.

推荐答案

这是非常宽松和不完整的-实际上是要作为一个起点:

This is pretty loose and incomplete -- and really is meant to be a starting point:

if (typeof $ != 'undefined') {
    var msg = 'This page already using jQuery v' + $.fn.jquery;
} else {
    var s = document.createElement('script');
    s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js');
    document.getElementsByTagName('head')[0].appendChild(s);
    var msg = 'This page is now jQuerified';
}

然后通过简短的setTimeout()等待,然后再运行ready()函数

then wait via a brief setTimeout() before running a ready() function

这篇关于小部件中的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆