使用document.writeln()重新定义jQuery [英] Redefine jQuery using document.writeln()

查看:107
本文介绍了使用document.writeln()重新定义jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我们的网站(> 8000)当前正在使用的Javascript库. 网站正在调用bootstrapper js,而该js会引用其他一些库脚本.

由于我们的JavaScript库大量使用jQuery,因此它目前也包含在引导程序中.

这是使用document.writeln完成的:

document.writeln("<script src=\"js\/jquery-1.4.4.min.js\" type=\"text\/javascript\"><\/script>");

我知道document.writeln不符合xHtml,并且有更好的解决方案,但这只是目前它对我们所有网站都适用的方式,而摆脱document.writeln则不是一种选择. /p>

现在的问题是,当网站已经定义了较低版本的jQuery时,我们会发生冲突.因此,我们决定在我们的库中重新定义jQuery:

document.writeln("<script src=\"js\/jquery-1.4.4.min.js\" type=\"text\/javascript\"><\/script>");
document.writeln("<script type=\"text\/javascript\">");    
document.writeln("   var $myJQuery = jQuery.noConflict();");
document.writeln("</script>");

不幸的是,它不起作用.

有人有解决方案吗?

解决方案

jQuery.noConflict()仅释放$()而不释放jQuery(),也许就是问题所在.您可以尝试手动执行此操作(但请参见下面的更好方法),如下所示:

<script>
  theOld$ = $;
  theOldjQuery = jQuery;
</script>
<script src=jquery-new-version.js></script>
<script>
  $ = theOld$;
  newjQuery = jQuery;
  jQuery = theOldjQuery;
</script>

更新:一种更好的方法(感谢T.J. Crowder的提示)是:

<script src=jquery-new-version.js></script>
<script>
  newjQuery = jQuery.noConflict(true);
</script>

请参阅 jQuery.noConflict 的文档.

I'm working on a Javascript library which is currently in use by our websites (> 8000). Websites are calling a bootstrapper js that on it's turn will reference some other library scripts.

Since our JavaScript library heavily uses jQuery, it's currently included in the bootstrapper as well.

This is done using document.writeln:

document.writeln("<script src=\"js\/jquery-1.4.4.min.js\" type=\"text\/javascript\"><\/script>");

I'm aware that document.writeln isn't xHtml compliant and there are much better solutions, but this is just the way it currently works fine for all our websites and getting rid of document.writeln is not an option.

The problem now is that we get conflicts when a site already defines a lower version of jQuery. So, we decided redefine jQuery in our library:

document.writeln("<script src=\"js\/jquery-1.4.4.min.js\" type=\"text\/javascript\"><\/script>");
document.writeln("<script type=\"text\/javascript\">");    
document.writeln("   var $myJQuery = jQuery.noConflict();");
document.writeln("</script>");

Unfortunately, it doesn't work.

Does anybody have a solution?

解决方案

The jQuery.noConflict() only frees the $() and not jQuery() and maybe that's the problem. You could try doing it manually (but see below for a better way) like this:

<script>
  theOld$ = $;
  theOldjQuery = jQuery;
</script>
<script src=jquery-new-version.js></script>
<script>
  $ = theOld$;
  newjQuery = jQuery;
  jQuery = theOldjQuery;
</script>

UPDATE: A better way to do the same (thanks to T.J. Crowder for the tip) would be:

<script src=jquery-new-version.js></script>
<script>
  newjQuery = jQuery.noConflict(true);
</script>

See the docs for jQuery.noConflict.

这篇关于使用document.writeln()重新定义jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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