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!

Javascript not running on AJAX

Status
Not open for further replies.

mpartarrieu

Programmer
Nov 21, 2006
34
ES
Hi. I'm releatively new to AJAX and was hoping someone could shed some light on this problem. I have this page with a link than changes the content of a div using AJAX. The new content includes is a PHP file with a javascript function and html, including a link to execute the javascript function. The problem is that when I click on the link, I get an error telling the javascript function is not defined.

I need to include the javascript function in the response text because some values of the function are retrieved from a database depending on the page you're retrieving, so it's not an option to call a general function outside of the AJAX response.

A simple example is available here:
Thanks in advance for your help.
 
When the response is returned to the browser, it is returned as a blob of text. You can set the innerHTML of a div to this blob or text, but it will not parse the contents as anything except HTML (ie: no javascript).

If you are returning a mix of javascript and HTML, then you will have to parse out the javascript and then add it in as a script block using DOM methods (I believe).

Is it not possible to have the function loaded on the initial page load (so it is already in the DOM when you click the link)?

You could look at JSON as a solution to this type of problem.

A starting point at least!

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
It is possible to have the function loaded on the initial page load as you suggest, BabyJeffy. I didn't want to do this because there are many variables used on the js function that are different for each page I call, so I hoped there was a way I could define the functions in the AJAX response.

However, using "eval" to define the variables might be a solution for this, as BillyRayPreachersSon suggests. I'll give it a try and come back with news (hope good ones).
 
However, using "eval" to define the variables might be a solution for this, as BillyRayPreachersSon suggests.

Not necessary to use eval() and tolerate the security risks.

There exists a public domain implementation of a JSON parser that obviates the need for eval(). It can be found here.

Tom Morrison
 
Dan,

I don't disagree. And JavaScript is reasonably safe, so an eval() should be reasonable safe. Just thought I would show that there is another way...

Tom Morrison
 
Not necessary to use eval() and tolerate the security risks.

There exists a public domain implementation of a JSON parser that obviates the need for eval(). It can be found here.

The funny thing is that the json parser included in that js file uses eval - it's just nice enough to verify the data for you beforehand.

In the OP's instance though, if he's returning javascript that needs to be ran (function declaractions and such) then the json parser will be of no help because it only understands json format - he will need to use the eval function instead.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
I solve the problem following Jeff and Dan's suggestions: define the javascript function within the index file and then use eval on the AJAX response to change the value of certain variables that work with this function. Works perfect. Thanks for the advice.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top