Prototype Ajax.Updater Eval Javascript Functions and HTML
I want to inject the myscript.php code into my handler page. Here are the source:
handler.php
var myArr = Array();
function myfunc(){
// some code
handleArray(myArr);
}
function handleArray(arr){
// some code
}
$("container").obse开发者_如何学编程rve('click', function(evt){
new Ajax.Request('myscript.php', {
method:'get',
evalScripts: 'true',
onFailure: function(e){
console.log(e);
},
onSuccess: function(t){
$("container").update(t);
myfunc(arr);
}
});
});
myscript.php
<script type="text/javascript">
var myArr = Array("hello", "world");
</script>
<div id="abc">some html code</html>
Basically I have my functions defined in my handler page, and myscript.php holds the necessary data for handler page to handle. While the above does make an Ajax request to myscript.php, the returned code does not get evaluated. It shows as Object Object instead of treating it as html. I ported the same code to jQuery and set datatype:"Script"
, everything works fine. While switching to jQuery is not an option to me since the entire code base is built on top of prototype, I would love to know how I can have prototype to treat the returned page as script.
I do noticed there is a similar thread Prototype Ajax.Updater Eval Javascript Functions that says wrapping the function as anonymous function and turning on evalJS flag will solve the problem, it doesn't seem like a workaround in my case as I have couple of variables and functions declared, I have no idea how to make all of them anonymous. Any help will be greatly appreciated and let me know if you need more info.
The evaluated script blocks will be processed in the scope of the Ajax functions, so...
var myArr = Array("hello", "world");
becomes a local variable in that function only. It is unavailable and discarded moments later. Make the returned script (myscript.php) update global myArr
instead.
myArr = Array("hello", "world");
精彩评论