Calling a javascript function in an ajax page

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?
LVL 2
astrohelpAsked:
Who is Participating?
 
gops1Commented:
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.
0
 
netsmithcentralCommented:
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 + '}');
0
 
astrohelpAuthor Commented:
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.

------------------------index--------------------------------
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");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

function sndReq2() {

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

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

            http.open('get', urlid_ext);
            http.onreadystatechange = handleResponse2;
          http.send(null);

}

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

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

//alert(response);
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
0
 
netsmithcentralCommented:
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.
0
 
astrohelpAuthor Commented:
...

ok...

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!

thanks!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.