Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7708653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:40:01+00:00 2026-06-01T00:40:01+00:00

I have an AJAX function inside a javascript file and I want it to

  • 0

I have an AJAX function inside a javascript file and I want it to be abstract and not to use any global variables.

The AJAX inserts an event into a database:

function insertCalendarEvents(calendar_group, event_name, event_datestart, event_datestop, event_timestart, event_timestop, event_info, onFinish) {
    var request;
    if(window.XMLHttpRequest)
        request = new XMLHttpRequest();
    else
        request = new ActiveXObject("Microsoft.XMLHTTP");

    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {
            if(request.responseText.substr(0, 6) == "error ")
                alert(errorName[request.responseText.substr(6)]);
            else {
                var event_id = request.responseText;
                //call onFinish function with parameter event_id which database assigned it
            }
        }
    }

    request.open("GET", "php/calendar.php?action=insertCalendarEvents&calendar_group=" + calendar_group + "&event_name=" + event_name + "&event_datestart=" + event_datestart + "&event_datestop=" + event_datestop + "&event_timestart=" + event_timestart + "&event_timestop=" + event_timestop + "&event_info=" + event_info, true);
    request.send();
}

The function is asynchronous so what I want to do is, at the moment it finished inserting the data into the database, I want it to execute a function which I will give it as a parameter.

If there is a better idea on how to accomplish this, I would be really happy to hear it 🙂

Thank you in advance, Daniel!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:40:03+00:00Added an answer on June 1, 2026 at 12:40 am

    Functions are just objects you can pass. The only extra thing is that you can call them using () (additionally with arguments, of course).

    You can pass them around just fine; all information such as scoping will be preserved. So, this should work:

    onFinish(event_id);
    

    Basically, a more trivial example would be this:

    function func() {
      alert("func is being run");
    }
    
    
    func();  // works
    
    
    function callFunction(f) {
      f();
    }
    
    callFunction(func);  // pass func; works
    
    
    callFunction(function() {             // pass a function on the fly; works
      alert("function passed directly");  // (nothing special in fact)
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple ajax call inside a javascript function to a php file,
I have a JavaScript function inside which I use jQuery .post, and I wanted
I have a jquery json ajax call and inside my success function I have
I have some jQuery code. I have called an Ajax function file, file.php, that
I have come across a website that appears to use Ajax but does not
i have the following Jquery function inside my view:- <script type=text/javascript> $(document).ready(function () {
I have a .csHtml -razor file with a javascript function that uses the @Url.Content
Have a form that is not being read by serialize() function. <script type=text/javascript> function
How can I use a custom tag inside my javascript code? For example: function
I have a jquery-ajax function that sends data to a php script and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.