Calling a javascript function in an ajax page

Posted on 2006-05-26
Last Modified: 2008-02-26
I have a page that I use some ajax to call another page and place the results into the innerhtml of a div. I want to be able to call a javascript function from the returned html. Is this possible?
Question by:astrohelp
    LVL 12

    Assisted Solution

    Sure.  A named function is really just a variable with that function stored in it.  Supposing you have the function prototype as the result of an AJAX query, you can assign it to a variable name (function name).  Example below:

    var httpRequest //AJAX call here

    eval('var funcName = function() { ' + httpRequest.ResponseText + '}');
    LVL 2

    Author Comment

    Hmm, I am a little rusty with js, so I think it will be better for everyone if I just put up a sample of my code.

    var ro;

    var http = createRequestObject();
    var urlid = "";
    var urlid_ext = "";

    function createRequestObject() {

        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer"){
            ro = new ActiveXObject("Microsoft.XMLHTTP");
            ro = new XMLHttpRequest();
        return ro;

    function sndReq2() {

                //urlid = url.substring(0,url.indexOf("."));

                urlid_ext = "/poll.php?action=display";

      'get', urlid_ext);
                http.onreadystatechange = handleResponse2;


    function handleResponse2() {
        if(http.readyState == 4){
            var response = http.responseText;
            //var update = new Array();

                //update = response.split('|');
               //response = response.replace('%0A','*');

    document.getElementById("pollarea").innerHTML = response;

    -----------------------------requested page----------------------------------
    echo("<input type=radio onclick='function()' name=opts value=" . $poll_order . ">" . $opts . "<br>");

    I would like the onclick in the requested page to call a function on the index page.

    thanks again
    LVL 12

    Expert Comment

    You can register event handlers to items in the DOM by referencing them as properties.
       eg - document.getElementById('pollarea').childNodes[childNodes.length].onclick = doSomething;  <--- notice you don't put () on the function as that would register the result to to the event, not the function itself.
    LVL 17

    Accepted Solution

    Another way is all Javascript functions made inside called HTML can be stored seperatly inside a common JS file or it can be placed inside the calling HTML.
    LVL 2

    Author Comment



    I was having a bad day when I wrote this question, It was one of those days when the obvious seems impossible. I am going to award the points to both of you, I have no idea why this was a problem to me last night!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now