Skip to content Skip to sidebar Skip to footer

Calling A Ajax Function On Page Load And On Click Of Button

I have a javascript file containing a ajax function with parameter x. I cannot reveal the function.I want to call the function on page load and on a button click.Also there is a fo

Solution 1:

Try doing this:

function f1() {
    //what you need to do
}
function f2() {
    //Do the same thing here also
}

This being your JS, in onload, call f1() and for button click call f2(). If you want f1() to execute before f2(), i suggest you look into locks here:

How to implement a lock in JavaScript

Solution 2:

Your javaScript code should be something like this:

varfn = functionfn(x) {
  // Do something
}

$(function() {
  var someValue;

  // someValue = ...// Call function once document is loadedfn(someValue);
  // Bind function as a callback to button click
  $('#button').on('click', function(event) {
    fn(someValue);
  });
});

Post a Comment for "Calling A Ajax Function On Page Load And On Click Of Button"