如果我通过书签包含 jQuery,它会破坏网站上的原始 JavaScript 吗?

发布于 2024-08-30 00:52:45 字数 98 浏览 7 评论 0原文

我正在创建一个书签,当然我想使用 jQuery。但是,如果我在网站上包含 jQuery(在头部附加脚本标签),如果网站上有其他 js,网站本身还能工作吗?

马蒂·莱恩

I'm creating a bookmarklet and of course I'd like to use jQuery. But, if I include jQuery (append a script-tag to head) on a site, will the site itself work anymore, if it has some other js on?

Martti Laine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-09-06 00:52:45

它会坏掉吗?...也许:)

在我的脑海中,有两个非常常见的原因会导致这种情况发生。如果他们使用另一个库,该库使用 $ 作为其主要对象,那么您将遇到冲突,但这可以通过 jQuery.noConflict(),像这样:

var $j = jQuery.noConflict(); //give `

它会坏掉吗?...也许:)

在我的脑海中,有两个非常常见的原因会导致这种情况发生。如果他们使用另一个库,该库使用 $ 作为其主要对象,那么您将遇到冲突,但这可以通过 jQuery.noConflict(),像这样:

back to whatever had it... $j("selector").doSomething(); //use the $j you assigned or jQuery from now on

另一个是他们已经加载了 jQuery(也可能是不同的版本) 。我只是在加载脚本之前添加一个 jQuery 对象测试,如下所示:

if (typeof jQuery == 'undefined') { //load jQuery }
//jQuery has been loaded, either by your or originally, keep going...

这个已加载的解决方案有一个警告,如果他们有 版本的 jQuery,你'将仅具有该版本的功能。我不知道有什么方法可以在页面中加载旧版本和新版本,并且不会出现很多奇怪的行为……它的设计初衷并不是为了做到这一点。

另外,对于 jQuery 已加载并且 $ 的情况,我会将这两种解决方法结合起来,如下所示:

if (typeof jQuery == 'undefined') {
  var used = typeof $ != 'undefined';
  //load jQuery 
  if(used) jQuery.noConflict();
}
window.$j = jQuery;
//always use $j in your script, it'll always be present.

Will it break?....maybe :)

There are 2 really common reasons off the top of my head that this would happen. If they're using another library that uses the $ for it's main object then you'll get a conflict, this however is resolvable with jQuery.noConflict(), like this:

var $j = jQuery.noConflict(); //give `

Will it break?....maybe :)

There are 2 really common reasons off the top of my head that this would happen. If they're using another library that uses the $ for it's main object then you'll get a conflict, this however is resolvable with jQuery.noConflict(), like this:

back to whatever had it... $j("selector").doSomething(); //use the $j you assigned or jQuery from now on

The other one would be they already have jQuery loaded (possibly a different version as well). I would just add a jQuery object test before loading your script, like this:

if (typeof jQuery == 'undefined') { //load jQuery }
//jQuery has been loaded, either by your or originally, keep going...

This solution for already loaded has one caveat, if they had an old version of jQuery, you'll only have the features of that version. I don't know of a way to load an old version and a new one in the page and not have a lot of weird behavior...it just wasn't designed to do that.

Also, I would combine these two work-arounds for the case that jQuery is already loaded and is the $, like this:

if (typeof jQuery == 'undefined') {
  var used = typeof $ != 'undefined';
  //load jQuery 
  if(used) jQuery.noConflict();
}
window.$j = jQuery;
//always use $j in your script, it'll always be present.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文