简体   繁体   English

使用jQuery动态导入JavaScript?

[英]Import javascript dynamically with jQuery?

I'm trying to setup a widget framework using jQuery, so I need to import java script by parsing the widget's xhtml and including in the framework page. 我正在尝试使用jQuery设置小部件框架,因此我需要通过解析小部件的xhtml并将其包含在框架页面中来导入Java脚本。 The problem is that this may lead to pollution of the namespace. 问题在于,这可能导致名称空间受到污染。 Is it possible to load java script into a namespace at runtime? 是否可以在运行时将Java脚本加载到名称空间中?

Perhaps there is some better method that I'm overlooking? 也许有一些更好的方法被我忽略了?

Thanks, Pete 谢谢,皮特

You can do something like this... 你可以做这样的事情...

var myOtherFramework = null;

$.get("myScript.js", function(data) {
  myOtherFramework = eval("(" + data + ")");
});

And then reference it like... 然后像引用...

myOtherFramework.funcName();

AS Josh Stodola mentioned creating the variable at runtime isn't the problem 正如Josh Stodola提到的,在运行时创建变量不是问题

var holdsWidgetUUID = "widgetUUIDValue";
eval(holdsWidgetUUID + "= (" + data + ")");
alert(eval(holdsWidgetUUID));

Or if you prefer 或者,如果您愿意

var UUID = "widgetUUID";
var holdsWidgetUUID = "widgetUUIDValue";
window["widgets"] = new Array();
window["widgets"][holdsWidgetUUID] = data;
alert(window["widgets"][holdsWidgetUUID]);

The problem is getting the loaded javascript to work an be callable like dynamicvariablename.methodname() 问题是让加载的javascript可以像dynamicvariablename.methodname()一样被调用

I have a working solution if you force a certain coding practice upon the included javascript. 如果您对随附的javascript进行某种编码实践,则我有一个可行的解决方案。 Maybe this gets you in the right direction. 也许这会使您朝着正确的方向前进。

This is a widgets javascript (works.js). 这是一个小部件javascript(works.js)。 Notive that it's a javascript "class" with internally defined fields and methods. 请注意,这是具有内部定义的字段和方法的javascript“类”。 Which by itself keeps namespace pollution low and allows us the achieve the desired calling form x.getInfo() 它本身使名称空间污染保持较低,并允许我们实现所需的调用形式x.getInfo()

function () {
    this.type = 1;
    this.color = "red";
    this.getInfo = function() {
        return this.color + ' ' + this.type + ' widget';
    };
}

And this is the file which includes it at runtime in a namespace 这是在运行时将其包含在名称空间中的文件

<html>
<head>
  <title>Widget Magic?</title>
  <script type='text/javascript' src='jquery.js'></script>
  <script type='text/javascript'>
    var UUID = "widgetUUID";
    var holdsWidgetUUID = "widgetUUIDValue";
    window["widgets"] = new Array();
    $.get("works.js", function(data) {
      window["widgets"][holdsWidgetUUID] = eval("(" + data + ")");
      $(document).ready(function() {
        window["widgets"][holdsWidgetUUID] = new (window["widgets"][holdsWidgetUUID])();
        alert(window["widgets"][holdsWidgetUUID].color);
        alert(window["widgets"][holdsWidgetUUID].getInfo());
      });
    });
  </script>
</head>
<body>
  <p>Just some stuff</p>
</body>
</html>

您加载的javascript本身应该已经在使用唯一的名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM