使用jQuery导入javascript动态? [英] Import javascript dynamically with jQuery?

查看:105
本文介绍了使用jQuery导入javascript动态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery设置一个widget框架,所以我需要通过解析小部件的xhtml并在框架页面中包含来导入java脚本。问题是这可能会导致命名空间的污染。运行时可以将java脚本加载到命名空间吗?



可能有一些更好的方法,我忽略了?



谢谢,
Pete

解决方案

AS Josh Stodola提到在运行时创建变量不是问题

  varholdersWidgetUUID =widgetUUIDValue; 
eval(heldWidgetUUID +=(+ data +));
alert(eval(heldWidgetUUID));

或者如果您喜欢

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

问题是如何加载javascript可以像dynamicvariablename.methodname()



如果您在包含的JavaScript中强制执行某种编码操作,我有一个工作的解决方案。也许这会让你走向正确的方向。



这是一个小部件javascript(works.js)。值得注意的是,它是一个具有内部定义的字段和方法的javascript类。它本身使命名空间污染较低,并允许我们实现所需的调用形式x.getInfo()

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

这是在运行时在命名空间中包含它的文件

 < html> 
< head>
< title> Widget Magic?< / title>
< script type ='text / javascript'src ='jquery.js'>< / script>
< script type ='text / javascript'>
var UUID =widgetUUID;
var heldWidgetUUID =widgetUUIDValue;
window [widgets] = new Array();
$ .get(works.js,function(data){
window [widgets] [heldWidgetUUID] = eval((+ data +));
$(document).ready(function(){
window [widgets] [heldWidgetUUID] = new(window [widgets] [heldWidgetUUID])();
alert(window [widgets ] [holdingWidgetUUID] .color);
alert(window [widgets] [heldWidgetUUID] .getInfo());
});
});
< / script>
< / head>
< body>
< p>只是一些东西< / p>
< / body>
< / html>


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. The problem is that this may lead to pollution of the namespace. Is it possible to load java script into a namespace at runtime?

Perhaps there is some better method that I'm overlooking?

Thanks, Pete

解决方案

AS Josh Stodola mentioned creating the variable at runtime isn't the problem

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()

I have a working solution if you force a certain coding practice upon the included javascript. Maybe this gets you in the right direction.

This is a widgets javascript (works.js). Notive that it's a javascript "class" with internally defined fields and methods. Which by itself keeps namespace pollution low and allows us the achieve the desired calling form 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>

这篇关于使用jQuery导入javascript动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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