javascript question?
- Started
- Last post
- 4 Responses
- n8w
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);
- sublocked0
AFAIK it doesn't work like that bruh...
you normally catch the event in your handler, then suss out the other shit from there. normally i'll add properties to the HTML element that i can snatch up using getAttribute. that's the best way to "pass arguments"
- n8w0
here is my ajax test
<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(qid) {
//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
mvar=qid;
//in this case simply assign the response to the contents of the <div> on the page.
document.getElementById(mvar).in... = 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>
- sublocked0
You wouldn't grab the div ID dynamically...What you want is to grab the event "source", then fuck with things from there.
Peep this pasted code:
I've shown examples for prototype.js (which I use now) and non-prototype.js (which I used to use).