同一页面上的多个版本的脚本(d3.js) [英] multiple versions of a script on the same page (d3.js)

查看:642
本文介绍了同一页面上的多个版本的脚本(d3.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在同一页面上有多个版本的JavaScript库。

I need to have multiple versions of a javascript library on the same page. How can I accomplish this, short of manually refactoring one version to avoid naming conflicts?

有很多关于如何使用Jquery做这个的例子( example )。这似乎依赖于jQuery goodness,但是。如何为任意脚本执行此操作?

There are many examples of how to do this with Jquery (example). This seems to rely on jQuery goodness, however. How can I do this for an arbitrary script?

更多详细信息:我使用 d3.js ,我插入他人使用d3做的可视化。问题是,其中一个vizzes需要一个版本的d3,另一个需要一个较新的版本。这两个vizzes应该是在同一个页面 - 用户交换,它通过点击缩略图显示,然后js用于隐藏一个viz和构建另一个。因此,似乎交换脚本而不是以无冲突的方式加载也可以是一个选项。

More detail: I'm using d3.js, and I'm plugging in visualizations others have made using d3. The issue is, one of the vizzes requires one version of d3, the other requires a newer version. Both of these vizzes are supposed to be available on the same page - the user swaps which viz is displayed by clicking a thumbnail, and then js is used to hide one viz and build the other. So, it seems like swapping the script rather than loading both in a no-conflict style could also be an option.

推荐答案

您可以查看主要的d3源文件: https://github.com/ mbostock / d3 / blob / master / d3.js

If you take a look at the main d3 source file: https://github.com/mbostock/d3/blob/master/d3.js

您会看到它开始:

d3 = function() {
  var d3 = {
    version: "3.1.5"
  };
  //.....

所以d3只是一个对象。我不知道这是否是最好的方法,但我认为以下将工作:

So d3 is just an object. I'm not sure if this is the best method, but I think the following would work:


  1. 包括一个版本的d3

  2. 添加下列行: d3versionX = d3;

  3. 包含下一个版本
  4. $ b
  5. 放置 d3 = d3versionX 其中X是可视化的默认版本当页面加载时

  6. 在触发版本切换的缩略图上放置事件处理程序,并将d3变量设置为适当的版本号作为第一件事情。

  1. include one version of d3
  2. put the line: d3versionX = d3;
  3. include the next version
  4. same as 2 with different version number.
  5. put d3 = d3versionX where X is your default version for the visualization when the page loads
  6. put an event handler on the thumbnails that trigger the switching of version, and set the d3 variable to the appropriate version number as the first thing that happens.

这篇关于同一页面上的多个版本的脚本(d3.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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