The problem with JavaScript code, received via an Ajax call, is that it is received as a text string and I'm not aware of any formal method for telling the browser to invoke the JavaScript Engine and, thus, turn that text string into executable JavaScript code. Did you notice that I said "formal" method, though. The only "informal" method I'm aware of (that works) is using the eval() method.
eval() is a very useful and easily abused function.
eval is your portal into the JScript / JavaScript engine, you poke it with VALID code and the interpreter will run that section of code, you can easily run into trouble with misuse of it, so you MUST limit its use and take care when using it...
Code:
var y = 'prompt("Hi what is your name?","");';
var x = eval(y);
document.write('Hello ' + x);
Thanks for your reply. But I still can't make this alert work with eval().
I modified my index.php script like this:
PHP Code:
<?php
print<<<EOF
<b>Call Alert</b>
<script language=javascript>
var y = 'prompt("Hi what is your name?","");';
var x = eval(y);
document.write('Hello ' + x);
</script>
EOF;
?>
I thought that you want a script returned by an AJAX request to be run in the browser.
if you take the responseText value that is returned and pass it to eval() the script will run if the code is VALID!
The example I p[osted was to demonstrate that the value in the string was script and that eval when it recieved it ran the script and returned a value.
that should be more than enough for you to achieve your goal.
Sorry to revive a VERY old topic, but it's relevant to my question:
I'm doing pretty much what the rest of this thread is describing. Once I get the readyState == 4 and have my response rendered, I call a function that gets the elements with name "scripts" in my response and then passes the innerHTML to eval.
This works if the innerHTML contains only alert("test"); but the problem is that it also prints the text "alert("test");" to the page, as well as evaluating it. If I wrap it in script tags it no longer print but doesn't execute either.
What's the right way of executing the script code without printing it to the page.
The JavaScript code you recieve in responseText is just text. To have the browser interpret the responseText as JavaScript you need to eval it. Its very simple...you just do
Code:
if (xhr.readyState == 4)
eval(xhr.responseText);
An example of how eval works
Code:
// A string
var s = 'function test(){alert("Hello world"};';
// Now eval the string
eval(s);
// The function test is now available
test(); // Output is 'Hello world'
I wasn't clear enough in my previous post, sorry. The problem is that I want part of the response rendered on the page, as well as some javascript on the page as well, that is generated from the PHP.
I got this working by wrapping the script sections in tags and then evaluating only those bits of the response, and then additionally also setting the display style to 'none' to stop it printing. If I don't set it to display:none then the script sections get evaluated by eval AND get printed on the page
I'm not sure this the correct way of doing it, but the results are what I need.
Bookmarks