Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert javascript using ajax

    I'm working on a project that needs the ability to insert javascript functions and code after the page is fully loaded. The content of the page is going to change dynamically as it is updated through ajax, and I need to be able to add javascript to manipulate new content on the page. For instance, I want to be able to add a button to the screen, and when I add that, add a javascript function to handle when the button is clicked. I have tried just adding it to the body of the document, but I still get an error saying that the function does not exist. I am using Firefox 2.0, which is the only browser it will ever need to run on, so firefox only fixes are ok.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why not just attach an onclick event in the button?
    Code:
    button.onclick = function(){
      alert('click');
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That would work for the buttons, but what if I need functions to be available to other processes already running on screen. For instance, if I wanted some recursive function added in later, there is no way to call it. I have tried adding in code by using eval on the full function name() {...}, but that doesn't seem to pull it in. I have added it in to its own script section as well. I have gotten everything working, but it is really messy and others wouldn't be able to add on later, so I'm trying to come up with a way to clean up the code.

    Thanks.

  • #4
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found a way to get it to work. I just made an iframe with 0 height and width. I then load in the javascript to that frame and access it through the frame.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You could also do this:
    Code:
    function addScript(jsFile){
      var js = document.createElement("script");
      js.setAttribute("type", "text/javascript");
      js.setAttribute("src", jsFile);
      document.getElementsByTagName("head")[0].appendChild(js);
    }
    ...
    addScript("js/sample.js");
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    JavaScript and images can be downloaded and added to a web page at any time. This has been true for as long as web pages have supported JavaScript. No "Ajax" or "iframe" processing is required as they are the FOUR main methods of retrieving information from the server without a page reload.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am facing the exact same problem, but don't quite understand the iframe method as described.

    Let me elaborate. I'm using PHP together with AJAX to output dynamic content. Part of this content is a piece of dynamic javascript code which is supposed to load together with the dynamic content, but we all know now that javascript snippets loaded dynamically through AJAX will not be executed. Any kind sirs with a good idea on how to go about this?

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Can you post a sample of the dynamic content?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    we all know now that javascript snippets loaded dynamically through AJAX will not be executed.
    //ex:
    function ajCallBack(resp){ eval( resp ) }; //for code/functions

    function ajCallBack(resp){ eval( " ( "+ resp +" ) " ) }; //for JSON anon Objects


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •