2011-10-17 76 views
0

我正在使用MooTools 1.4。如何在我的页面上动态添加脚本标记?从下面的阿贾克斯requset的响应应该包含标签...如何在我的Ajax请求中包含Javascript脚本标记?

window.addEvent('domready', function() { 
    $('submit').addEvent('click', function(event) { 
     var filename = $('filename').value; 
     //prevent the page from changing 
     event.stop(); 
     //make the ajax call 
     var req = new Request({ 
      method: 'get', 
      url: 'renderpage?id=' + escape(filename), 
      data: { }, 
      evalScripts: true, 
      onRequest: function() { 
       // on request 
      }, 
      onComplete: function(response) { 
       $('content').set('html',response); 
       // Add the Ajax call where we save the data. 
       ... 
       }); 
      } 
     }).send(); 
    }); 
}); 

但是,当我看着响应,之后就被去掉了。我希望他们包括和评估。我怎样才能做到这一点?

+0

你是如何看的响应?浏览器在这种情况下不会去掉'

2

使用Request.HTML

var req = new Request.HTML({ 
     url: 'renderpage?id=' + escape(filename), 
     update: $('content'), 
     onRequest: function() { 
      // on request 
     } 
    }).get(); 
+0

是你不解释为什么downvote ?? : -/ – MattiSG

+0

+1几年后...并且您向我提供了一个非常干净的解决方案!谢谢,这应该是upvoted更多,是被接受的答案! – Shautieh

-1

传递给onComplete的参数是:

nodeTree, xhr, responseHTML, responseScripts

所以:

new Request.HTML({ 
    // ... 
    onComplete : function(nodeTree, xhr, responseHTML, responseScripts) { 
    eval(responseScripts); 
    } 
}); 
+0

为什么'eval'他们?阅读[HTML.Request文档](http://mootools.net/docs/core/Request/Request.HTML):_“evalScripts - (布尔:默认为true)如果设置为true,脚本标签内的响应将被评估。这覆盖了来自Request的虚假默认值。“_ – MattiSG

相关问题