thewooleymammoth Posted February 7, 2011 Share Posted February 7, 2011 I am creating a page that loads new content via ajax, but i need to execute a javascript function based on the content. ex <div id='content'> <script type='text/javascript'> function newFunction(){ alert('first function'); } </script> <p>here is content 1</p> </div> Then i want to change the contents of the page div, with ajax, to the following: <script type='text/javascript'> function newFunction(){ alert('second function'); } </script> <p>here is content 2</p> then call the newFunction() on a user event. but i need the function performed to match the function currently loaded inside the #page div. However, the function called is always the one first loaded ex: loadpage->newFunction returns "first function" -> load new function via ajax->newFunction still returns "first function" Any idea on how to achieve a return of second function after the ajax interaction? PS: im using jquery. Quote Link to comment https://forums.phpfreaks.com/topic/226990-jquery-changing-an-js-function-based-on-data-returned-from-ajax-call/ Share on other sites More sharing options...
thewooleymammoth Posted February 7, 2011 Author Share Posted February 7, 2011 please let me know if it is unclear Quote Link to comment https://forums.phpfreaks.com/topic/226990-jquery-changing-an-js-function-based-on-data-returned-from-ajax-call/#findComment-1171125 Share on other sites More sharing options...
jcanker Posted February 12, 2011 Share Posted February 12, 2011 With the little bit posted here, it seems to me like you're going about it backward. Are you putting the script inside a div? Don't do that. Put all the functions in a separate file linked in your <head>. As far as running one function instead of another, which I think you're trying to do, you have to look at the existing situation in the script and evaluate it. Set up an if statement or switch for more than two possibilites which will evaluate the situation and run the proper function, even if that if is something simple like (psuedocode): if div K is hidden, run function A. Else run function B. All of that code, however, should be in a separate file and kept together there. Quote Link to comment https://forums.phpfreaks.com/topic/226990-jquery-changing-an-js-function-based-on-data-returned-from-ajax-call/#findComment-1173324 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.