[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Calling a javascript function in an ajax page

Posted on 2006-05-26
Medium Priority
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
  • 2
  • 2
LVL 12

Assisted Solution

netsmithcentral earned 1000 total points
ID: 16773633
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 + '}');

Author Comment

ID: 16773726
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";

            http.open('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

ID: 16773820
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

gops1 earned 1000 total points
ID: 16773909
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.

Author Comment

ID: 16777454


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!


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

873 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