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 6891293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:23:30+00:00 2026-05-27T06:23:30+00:00

How can I use the callback function properly cause it’s not working. I want

  • 0

How can I use the callback function properly cause it’s not working.
I want to retrieve my json values from the server to the client side.

For starters I tried this to see if its working.

sample code

index.html

<button onClick = "Json()">Click Here</button>

my.js

function Json() 
{ 
var url = "http://www.sample.com/test.js?callback=displayUser";
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);   

} 

function displayUser(json) 
{
    alert("working");

}

test.js

var obj = { "a": "hey", "b": "what?"}

but does not return any alert, so I guess it does not proceed to my displayUser function.

  • 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-05-27T06:23:30+00:00Added an answer on May 27, 2026 at 6:23 am

    If you want displayUser() called in test.js, you have to have code in test.js that will call it. It won’t get called just because you put it in the URL. You would need to code parse it out of the URL and then call it.

    Or, alternatively, you can hook up a notification when the script has finished loading and just call displayUser() yourself from outside of test.js after test.js is fully loaded.

    To detect when the external script file has loaded successfully and just call displayUser() yourself, see this reference.

    If you want to get the callback value from the test.js script file itself and execute it from within test.js, this is the only way I know of to do it:

    window.displayUser = function() {
        // do whatever
    }
    
    var scripts = document.getElementsByTagName("script");
    for (var i = 0; i < scripts.length; i++) {
        var url = scripts[i].src;
        // extract filename and callback parameter from the URL
        var matches = url.match(/\/([^\/]+)\?.*callback=(.*?)(&|$)/);
        if (matches && matches[1] == "test.js") {
            // call the callback function
            window[matches[2]]();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

jQuery's getScript function doesn't seem to support an error callback function. I can't use
Current Asynchronous JavaScript calls require us to use a Callback function. This can lead
I know that I can use a Mouse callback function for when a user
I want to use a block as a callback function. I store my block
Normally, I can use this , in the callback function: $this->form_validation->set_message(_custom, My custom message.);
I use a 3rd party library, that does callback operations. It has a function
I'm templatizing a queue class so I can use it with anything from ints
I have refactored working code to use proper objects and now I can't get
I know I can use watch to bind a callback that will be triggered
I am very new to JavaScript and need to use callback function in my

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.