Javascript的AJAX包含文件witth EVAL [英] Javascript AJAX include file witth eval

查看:146
本文介绍了Javascript的AJAX包含文件witth EVAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

1) 一个HTML文档。

1) a HTML document.

2) 这个HTML文档载入JavaScript文件code.js是这样的:

2) This HTML document loads Javascript file "code.js" like this:

<script src="code.js">

3) 用户点击按钮,运行fetchdata功能中的code.js

3) User clicks button which runs "fetchdata" function in "code.js",

4) fetchdata的功能是这样的:

4) "fetchdata" function looks like this:

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState==4) {
        myjsdata = xmlhttp.responseText;
    }
}
xmlhttp.open("GET", 'http://www.example.com/data.js', false);
xmlhttp.send(null);

...

现在我该怎么办下列成功:

Now how do I do the following successfully:

我要的方式插入/ EVAL我的Javascript,以code.js,让所有功能,包括fetchdata和上/下可以访问数据定义函数(结构,报关,pre -calculated数据值等)中的data.js。

I want to insert/eval my Javascript in a way, so all functions in "code.js" including "fetchdata" and functions defined above/below can access the data (structures, declarations, pre-calculated data values etc.) in "data.js".

(如果这是可能的,这将是想法,因为我可以等待装载的实际JS数据文件,直到用户明确要求它。)

(If this was possible, it would be idea since I could wait loading the actual JS data file until the user explicitly requests it.)

推荐答案

jQuery的总是有一些东西的一切:

jQuery always has something for everything:

http://api.jquery.com/jQuery.getScript/

加载从URL的JavaScript文件,并在全球范围内执行。

Loads a javascript file from url and executes it in the global context.

编辑:哎呀,没有看到你没有使用jQuery。每个人总是使用jQuery ...

edit: Oops, didn't see that you weren't using jQuery. Everyone is always using jQuery...

只要做到:

var scrpt = document.createElement('script');
scrpt.src='http://www.example.com/data.js';
document.head.appendChild(scrpt);

这篇关于Javascript的AJAX包含文件witth EVAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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