Martin Bialasinski wrote:
should rather be
evalScriptsGlobal: function() {
return this.extractScripts().map(function(s){eval.call(window, s)});
},
Thanks to all who have supplied input on this.
To recap the problem that I would like to see a universal solution for:
(I think the OP was also looking for this)
I want to be able to load javascript functions (possibly a lot of code)
into an existing page in response to a user action and have that code be
executable from that point forward. That is, the page loads, the user
does something, the page loads new javascript from the server that
implements new event handlers, the user can then take actions that
execute these freshly loaded functions, all without reloading the entire
page.
The holy grail is that this should work for Mozilla, IE, Opera, and
Safari, on all of their respective platforms.
To do this something that holds the new functionality must be added into
the global space in such a way that it remains available after the
loading is complete. It would be nice if this persistent container were
detectable so that reloading could be avoided if the user invokes the
load operation a second time.
I have not yet seen such a universal solution.
Suggestions I have seen so far:
I proposed a function that adds a script element to the DOM. This works
for Mozilla, IE and Opera under Windows, but does not work for Safari
because Safari does not support createElement.
Someone suggested using document.write to write a new script element. I
don't see how this helps because I am not aware of any way to write to a
completed page without overwriting the page. This appears to me to be
simply a page reload.
Martin Bialasinski suggested a modified version of prototype.js's
evalScripts. This looked promising, but as far as I can tell it does not
work in IE. I have included a test sample below that uses it, and it
works in Mozilla and Opera, but not in IE.
Is there any universal solution to this?
-- Will Merrell
=================== Test Page begin =========================
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Eval Test</title>
<script type="text/javascript"
src="functions/javascript/prototype.js"></script>
<script type="text/javascript">
<!--
Object.extend(String.prototype, {
evalScriptsGlobal: function() {
return this.extractScripts().map(function(s){eval.call(window, s)});
}
});
function loadScripts(scrCode)
{
alert("Loading " + scrCode);
newscript = "<script type=\"text/javascript\" >" + scrCode +
"<\/script>\n";
newscript.evalScriptsGlobal();
alert(newscript);
}
function loadit()
{
loadScripts($("textarea_1").value);
}
// -->
</script>
</head>
<body>
<textarea id="textarea_1" rows="10" cols="80"
name="textarea1"></textarea><br />
<button onclick="loadit();" >Load It</button>
<button onclick="testit();" >Test It</button>
<script type="text/javascript">
<!--
// This script simply preloads the text area to save time.
$("textarea_1").value = "function testit() { alert(\"Hello from
TestIt\"); }\n";
// $("textarea_1").value = "testit = function() { alert(\"Hello from
TestIt\"); }\n";
// -->
</script>
</body>
</html>
=================== Test Page end=========================
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs