原型的Ajax.Updater评估和演示JavaScript函数 [英] Prototype Ajax.Updater Eval Javascript Functions

查看:223
本文介绍了原型的Ajax.Updater评估和演示JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个示例页面:

 < HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<冠军> AJAX页面< /标题>
<脚本类型=文/ JavaScript的>
功能ajax_hello(){
    警报(你好);
}
警报(你好,从JS);
< / SCRIPT>
< /头>

<身体GT;
这是Ajax页面。
< A HREF =#的onclick ='ajax_hello();'>点击这里断火的JS函数和LT; / a取代。
< /身体GT;
< / HTML>
 

我打电话用这样的:

 新的Ajax.Updater($(元素),页面,{方法:取,evalScripts:真});
 

警报正在运行,但功能不注册(ajax_hello())。

有没有办法让AJAX注册一个JavaScript函数来调用页面?

解决方案

在回应评论,我戳在文档并似乎有原型一些特殊的规则时,脚本由更新评估。该脚本可以在任何地方的响应,但是您需要分配任何函数定义一个全局变量来使它们对您的页面可用。

  

关于evalScripts和定义   功能如果使用evalScripts:   如此,任何块会   评估。这并不意味着它会   不,他们将:获得包含在页面中。   其内容就可以简单地传递给   本机eval()函数。有   两个后果,以这样的:

     

在本地范围将是的   样机的内部处理   功能。任何在你的脚本   使用var声明将被丢弃   评估后的瞬间,并在   无论如何,将是不可见的   其余页面脚本。如果你   在那里定义功能,你需要   实际上创建它们,否则它们   将无法访问到余   的页的脚本。即,   下面code是行不通的:

  //这种脚本不会,如果处理过的Ajax.Updater工作:
传播coolFunc(){
    //大放异彩!
}
 

您   将需要使用的语法如下:

  //这种脚本会工作,如果处理过的Ajax.Updater:
coolFunc =功能(){
   //大放异彩!
}
 

I have this sample page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ajax Page</title>
<script type="text/javascript">
function ajax_hello() {
    alert ("hello");	
}
alert ("Hello from JS");
</script>
</head>

<body>
This is the Ajax page.
<a href='#' onclick='ajax_hello();'>Click here to fire off JS function</a>.
</body>
</html>

I am calling it with this:

new Ajax.Updater($(element), page, { method: "get", evalScripts: true });

The alert is running, but the function is not registering (ajax_hello()).

Is there a way to get ajax to register a javascript function to the calling page?

解决方案

In response to the comment, I poked at the documentation and it appears that there are some special rules for Prototype when scripts are evaluated by the updater. The scripts can be anywhere in the response, but you need to assign any function definitions to a global variable to make them available to your page.

About evalScripts and defining functions If you use evalScripts: true, any block will be evaluated. This does not mean it will get included in the page: they won't. Their content will simply be passed to the native eval() function. There are two consequences to this:

The local scope will be that of Prototype's internal processing function. Anything in your script declared with var will be discarded momentarily after evaluation, and at any rate will be invisible to the remainder of the page scripts. If you define functions in there, you need to actually create them, otherwise they won't be accessible to the remainder of the page scripts. That is, the following code won't work:

// This kind of script won't work if processed by Ajax.Updater:
function coolFunc() {
    // Amazing stuff!
}

You will need to use the following syntax:

// This kind of script WILL work if processed by Ajax.Updater:
coolFunc = function() {
   // Amazing stuff!
}

这篇关于原型的Ajax.Updater评估和演示JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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