Jump to content

Recommended Posts

in javascript how do you assign an event handler with arguments

 

it works like this ... but I would like to pass arguments to the processResponse function

  http.onreadystatechange = processResponse;

 

but ideally I would like to do something like this

http.onreadystatechange = processResponse(argument1, argument2, etc);

Link to comment
https://forums.phpfreaks.com/topic/138310-event-handler-with-arguments/
Share on other sites

Here is the code .. I am trying to figure out how I can get the processResponse() function the qid? because the event handler doesn't allow me to pass arguments

http.onreadystatechange = processResponse;

 

but I need to so it knows which div to pass the response back to

document.getElementById(qid).innerHTML = response;

<html>
<body>

<script language="Javascript" type="text/javascript">
<!--

function createRequestObject() {
    var tmpXmlHttpObject;
    
    //depending on what the browser supports, use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) { 
        // Mozilla, Safari would use this method ...
        tmpXmlHttpObject = new XMLHttpRequest();

    } else if (window.ActiveXObject) { 
        // IE would use this method ...
        tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    return tmpXmlHttpObject;
}

//call the above function to create the XMLHttpRequest object
var http = createRequestObject();

function makeGetRequest(qid) {
    //make a connection to the server ... specifying that you intend to make a GET request 
    //to the server. Specifiy the page name and the URL parameters to send
    http.open('get', 'ajax2.php?q_user_id=' + qid);

    //assign a handler for the response
    http.onreadystatechange = processResponse;

    //actually send the request to the server
    http.send(null);
}

function processResponse() {
    //check if the response has been received from the server
    if(http.readyState == 4){

        //read and assign the response from the server
        var response = http.responseText;

        //do additional parsing of the response, if needed
        //in this case simply assign the response to the contents of the <div> on the page. 
        document.getElementById(qid).innerHTML = response;


        //If the server returned an error message like a 404 error, that message would be shown within the div tag!!. 
        //So it may be worth doing some basic error before setting the contents of the <div>
    }
}

-->
</script>

<h1>Have you heard these terms before?</h1>
<p>
Ceraunophobia <a href="javascript:makeGetRequest(1699)">More about Ceraunophobia</a><br>
Astraphobia <a href="javascript:makeGetRequest(2245)">More about Astraphobia</a><br>
Ophidiophobia <a href="javascript:makeGetRequest(3547)">More about Ophidiophobia</a><br>
</p>

<div id="f1699"></div> 


</body>
</html>

 

 

you can do it like so:

function hitch ( fcn ) {
  var args = [];
  for(var x=1; x<arguments.length; x++)
    args.push(arguments[x]);
  return function(){
    var ta = [];
    for(var x=0; x<arguments.length; x++)
      ta.push(arguments[x]);
    ta = ta.concat(args)
    return fcn.apply(this, ta);
  };
}

function processResponse ( response, arg1, arg2 ) {
  alert("Response: "+response+"\nArg 1: "+arg1+"\nArg 2: "+arg2);
}
http.onreadystatechange = hitch(processResponse,'abc','def');

  • 2 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.