使用XMLHTTP加载Javascript ...? [英] Loading Javascript with XMLHTTP...?

查看:66
本文介绍了使用XMLHTTP加载Javascript ...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XMLHTTP和DOM我可以加载新的HTML页面内容。


我现在想用HTML标记加载javascript的小片段

并且具有< script>纳入页面。如果任何加载的

脚本存在于函数定义之外(例如:对函数的调用),

我希望该代码尽快执行它被添加到DOM中。


有人能建议最好的方法吗?我用谷歌搜索但没找到

任何全面的东西。我需要使用eval()方法还是

a更好的方式?


谢谢,


史蒂夫

Using XMLHTTP and DOM I''m able to load new HTML page content.

I''d now like to load small snippets of javascript with the HTML markup
and have that <script> incorporated into the page. If any of the loaded
script exists outside a function definition (eg: a call to a function),
I''d like that code to be executed as soon as its added to the DOM.

Can anyone suggest the best way to do this? I''ve Googled but not found
anything comprehensive. Do I need to use the eval() method or is there
a better way?

Thanks,

Steve

推荐答案

sn **** @ mxlogic.com 写道:
使用XMLHTTP和DOM我能够加载新的HTML页面内容。

我现在想加载小带有HTML标记的javascript片段
并且具有< script>纳入页面。如果任何加载的
脚本存在于函数定义之外(例如:对函数的调用),
我希望该代码在添加到DOM后立即执行。 />
任何人都可以建议最好的方法吗?我用谷歌搜索但没找到
任何全面的东西。我需要使用eval()方法还是有更好的方法?

谢谢,

Steve
Using XMLHTTP and DOM I''m able to load new HTML page content.

I''d now like to load small snippets of javascript with the HTML markup
and have that <script> incorporated into the page. If any of the loaded
script exists outside a function definition (eg: a call to a function),
I''d like that code to be executed as soon as its added to the DOM.

Can anyone suggest the best way to do this? I''ve Googled but not found
anything comprehensive. Do I need to use the eval() method or is there
a better way?

Thanks,

Steve




最好的方法是添加一个带有src属性的脚本元素:


var oS = document.createElement(''script'');

oS.type =''text / javascript'';

oS.src =''someLink / cmd.js'';


< URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/f39a6a56185a49c1/a4c3d5dcce0a08e6?tvc = 1& q = document.createElement(%2 7script%27)& hl = en#a4c3d5dcce0a08e6>


但并非所有浏览器都支持上述内容。还要注意在任何链接文件中调用

函数,因为它们必须下载并且在

文档准备好使用之前添加到文档中。添加一个脚本元素然后

取决于它的内容是否立即可用。


在加载脚本时,将执行函数外的任何语句。 br />

AFAIK,你不能动态地修改脚本元素的内容。

你可以克隆一个脚本元素,它具有函数外的语句和

将它添加到文档的某些部分,但语句不会执行(至少不在Firefox或IE中)。

-

Rob



The best way is to add a script element with a src attribute:

var oS = document.createElement(''script'');
oS.type = ''text/javascript'';
oS.src = ''someLink/cmd.js'';

<URL:http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/f39a6a56185a49c1/a4c3d5dcce0a08e6?tvc=1&q=document.createElement(%2 7script%27)&hl=en#a4c3d5dcce0a08e6>

But not all browsers may support the above. Also be careful of calling
functions in any linked file as they must be downloaded & added to the
document before they are ready to use. Adding a script element and then
depending on its content being immediately available is risky.

Any statements outside functions will be executed when the script is loaded.

AFAIK, you can''t modify the content of a script element dynamically.
You can clone a script element that has statements outside functions and
add it to some part of the document, but the statements will not be
executed (at least not in Firefox or IE).
--
Rob


我可能错了,但您可能还需要设置延迟

属性为true。


oS.defer =" true";

I may be wrong about this, but you may also need to set the "defer"
attribute to true.

oS.defer="true";




< sn **** @ mxlogic.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

<sn****@mxlogic.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
使用XMLHTTP和DOM我可以加载新的HTML页面内容。

我现在想用HTML标记加载javascript的小片段并且有< script>纳入页面。如果任何加载的
脚本存在于函数定义之外(例如:对函数的调用),
我希望该代码在添加到DOM后立即执行。 />
任何人都可以建议最好的方法吗?我用谷歌搜索但没找到
任何全面的东西。我需要使用eval()方法还是有更好的方法?

谢谢,

Steve
Using XMLHTTP and DOM I''m able to load new HTML page content.

I''d now like to load small snippets of javascript with the HTML markup
and have that <script> incorporated into the page. If any of the loaded
script exists outside a function definition (eg: a call to a function),
I''d like that code to be executed as soon as its added to the DOM.

Can anyone suggest the best way to do this? I''ve Googled but not found
anything comprehensive. Do I need to use the eval() method or is there
a better way?

Thanks,

Steve




你可以在你的页面上添加一个隐藏的iframe,

并加载你想要的任何url。
在iframe页面底部的
,把一些

脚本可以执行某些操作,例如在主页面上调用

函数。



you could add a hidden iframe to your page,
and load whatever url you want into it.
at the bottom of the iframe page, put some
script that does something, such as call a
function on the main page.


这篇关于使用XMLHTTP加载Javascript ...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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