简体   繁体   English

Javascript评估问题

[英]Javascript eval question

I have page A that issues an ajax call and brings in snipped B. This snippet is being added into the DOM and all the scripts in that snipper are eval-ed. 我有一个页面A,该页面发出ajax调用,并带来了已删除的B。此代码段已添加到DOM中,并且已评估了该代码段中的所有脚本。 In that snippet, I have 2 script tags as such: 在该代码段中,我有2个脚本标签,例如:

<script type="text/javascript">
function doOptions(){
   alert('doOptions');
}
</script>
<script type="text/javascript">
   X = { 
      x : function(x) {
           alert('x');
      }


   }
</script>

Then the JS that is declared in the above script tags is being used on within snippet B as such: 然后,在上面的脚本标签中声明的JS会在代码段B中这样使用:

  <button type="button" onclick="doOptions();"> options </button>       
  <button type="button" onclick="X.x();"> XX </button>

Clicking on the XX button work, but clicking on the options button, does not. 单击“ XX”按钮有效,但单击“选项”按钮无效。 Both firefox and IE tell me that doOptions is not defined. Firefox和IE都告诉我doOptions没有定义。 Why? 为什么?

Also, what category of Javascript knowledge is this? 另外,这是什么类别的Javascript知识? Meaning, if I want to read more about this, what do I search for, where do I look in the table of contents in a JS book? 意思是,如果我想了解更多有关此的内容,我将搜索什么,在JS书的目录中应该在哪里寻找?

Thanks. 谢谢。

This snippet is being added into the DOM and all the scripts in that snipper are eval-ed. 该代码段已添加到DOM中,并且评估了该代码段中的所有脚本。

If you are using innerHTML to insert the script , it will not work - the script content will not be parsed. 如果您正在使用innerHTML插入脚本 ,则它将无法正常工作-不会解析脚本内容。 There are methods to get it working with Internet Explorer , but you'll never have a cross browser solution. 有一些方法可以使其与Internet Explorer一起使用 ,但是您永远不会拥有跨浏览器解决方案。 You should look at returning the data from the AJAX request in a different format (as text, for example) and then creating and appending the script element using DOM functions. 您应该查看以不同的格式(例如,以文本形式)从AJAX请求返回的数据,然后使用DOM函数创建和附加脚本元素。 For example: 例如:

// Create the script element
var script = document.createElement("script");
script.type = "text/javascript";

// Set the source as the location of the ajax service
script.src = "http://path.to/myajaxhandler.php?ajaxopt=2&anotheropt=5";

// Add the script element
document.getElementsByTagName("head")[0].appendChild(script);

Either that or parse out the text content from the script elements returned in the AJAX call, but that gets more complicated because you'll need to use regular expressions (which aren't ideal for parsing HTML, but might be ok if the content returned isn't too complex). 要么从AJAX调用中返回的脚本元素中解析出文本内容,但这会变得更加复杂,因为您将需要使用正则表达式(对于解析HTML而言,这不是理想的选择,但如果返回了内容,则可以不太复杂)。

If my guess is right and your problem falls this case, this might be useful. 如果我的猜测是正确的,而您的问题属于这种情况,这可能会很有用。 The problem could be due to the reason that the evals are executed in the calling environments context. 该问题可能是由于评估在调用环境上下文中执行的缘故。

I executed the following test: 我执行了以下测试:

function test() {
    eval('x={bar:function(){alert("bar");}}');
    eval('function foo(){alert("foo")}')


}
test();
x.bar(); //succeeds
foo(); //fails.

Here the call foo() fails because it is local to the test() function whereas the variable 'x' is a global variable(since 'var' is not specified) hence the call to bar() on x succeeds; 这里的foo()调用失败了,因为它是test()函数的局部变量,而变量'x'是全局变量(因为未指定'var'),因此x上对bar()的调用成功了;

If you want to make 'foo' function available outside, 如果您想在外部使用“ foo”功能,

function test() {
    eval('x={bar:function(){alert("bar");}}');
    eval('foo = function(){alert("foo")}')


}
test();
x.bar();
foo();

Now we are assigning the function to a global variable ('var' not specified). 现在,我们将函数分配给全局变量(未指定“ var”)。 Now you can call foo() function outside. 现在,您可以在外部调用foo()函数。

Hope this helps. 希望这可以帮助。

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

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