Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I execute Javasript back from a Ajax call

Status
Not open for further replies.

FoxAll

Programmer
Jun 13, 2003
49
CA
Hello All,

My program recuperate a complete HTML FORM from a ajax function.
Within this code, there are a javascript code to set the focus on the right field.
All html form and javascript code was transfered correctly but are never executed.

here an example of :
Code:
<div id="ajaxform">
<div id="myform">
<form>
 ....
</form>
<div id="ajaxscript">
<script type="text/javascript">
document.myform.object.focus();
</script>
</div>
</div>
</div>

at the start, "ajaxform" div is empty until ajax populate.
How can I execute Javasript back from a ajax call ?

Thank you
Harry
 
Are you sure it's not? Add an alert.

<script type="text/javascript">
alert("hello");
document.myform.object.focus();
</script>

-Geates
 
The Js returned from the ajax call is not part of the active DOM, its essentially just text, so it doesn't get executed.

If you need to execute something when the ajax call returns, have it at that point, rather than in the HTML returned by it.

That is you should not be returning JS in your AJAX call, only HTML. All your JS logic should be present in the base page and executed as needed.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
OK,

another one solved !!!
I'm new with Javascript/Ajax but I got this one!

all you hove to do is removing the script tag

like this
Code:
<div id="ajaxform">
<div id="myform">
<form>
 ....
</form>
<div id="ajaxscript">
document.myform.object.focus();
</div>
</div>
</div>


and then .... whene you recieve the Ajax result :

Code:
...
document.getElementById("div_form").innerHTML=xmlhttp.responseText;
var myscript = document.getElementById("ajaxscript").innerHTML ;
eval(myscript);
...

so that's it!
It works find .... but I hope it's cross browser? :)

thank you for you help
Harry
 
For the love of <insert Deity of your choice here> do not do this!

There are enough insecure garbage web site out their already.
Please repeat after me Eval is evil and should never be used.

Your Server side code should only be returning data that is then processed by the existing javascript in your page.
it is possible for the Ajax requests to be intercepted and spoofed with malicious code, returned data should always be validated just the same as all data received from the user (you are validating user data aren't you?)


A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
IPGuru, very good point. How is one to execute js controls created by an ajax call?

-Geates
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top