Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Javascript not running on AJAX

mpartarrieu (Programmer)
20 Sep 07 6:30
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: http://www.trobata.com/test/index.html

Thanks in advance for your help.
BabyJeffy (Programmer)
20 Sep 07 7:21
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

Jeff's Blog @ CodeRambler
http://www.coderambler.com/
http://www.coedit.co.uk/


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

FAQ216-6094: What is Javascript?

BillyRayPreachersSon (Programmer)
20 Sep 07 8:36

It might be worth considering using "eval" to run the JS, if it's only a small amount.

This is one of the few times i'd advocate using eval!

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch
http://www.codecouch.com/dan/


mpartarrieu (Programmer)
20 Sep 07 8:50
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).
k5tm (Programmer)
20 Sep 07 10:51

Quote:

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
www.liant.com

BillyRayPreachersSon (Programmer)
20 Sep 07 11:11

If known code is being sent back, there are no security risks as far as the OP is concerned.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page @ Code Couch
http://www.codecouch.com/dan/


k5tm (Programmer)
20 Sep 07 11:55
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
www.liant.com

kaht (Programmer)
20 Sep 07 12:59

Quote:

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

mpartarrieu (Programmer)
21 Sep 07 3:51
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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close