Jump to content

How to get variables from caller


Darghon

Recommended Posts

Hello all,

 

I'm creating an ajax class in javascript that creates the xmlrequest object, calls it, gets the returned value, parses it all in the a tree structure, but then when I try to put it all the target, I get the notice that target doesn't exists

so, how can I get my target var

 

I hope the code will be clear:

 

function Ajax(url, method, target, param, returnType){
var xml = createXMLHttpRequest();
this.url = (typeof(url) != "undefined")?url:"";;
this.method = (typeof(method) != "undefined")?method:"post";
this.target = (typeof(target) != "undefined")?target:null;
this.param = (typeof(param) != "undefined")?param:"";
this.returnType = (typeof(returnType) != "undefined")?returnType:"html";

/* List of class functions */
this.setUrl = function (url){ this.url = url; }
this.getUrl = function (){ return this.url; }
this.setMethod = function (m){ if(m.toLowerCase() == "post" || m.toLowerCase() == "get"){this.method = m.toLowerCase();}else{alert("invalid method.\n can only be Post or Get.");} }
this.getMethod = function (){ return this.method; }
this.setTarget = function (target){ if(typeof(target) != "undefined"){ this.target = target; }else{ alert("undefined object passed as target."); } }
this.getTarget = function (){ return this.target; }
this.setParam = function (param){ this.param = param; }
this.getParam = function (){ return this.param; }
this.setReturnType = function(type){ this.returnType = type; }
this.getReturnType = function(){ return this.returnType; }
this.getState = function(){ return xml.readystate; }


this.start = function(){
	if(this.method == "get"){
		var urlToSend = this.url;
		if(this.param != ""){
			urlToSend += "?" + this.param;
		}
		xml.open("GET", urlToSend);
		xml.send();
	}
	else if(this.method == "post"){
		xml.open("POST", this.url);
		xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
		xml.send(this.param);
	}
}

xml.onreadystatechange = function stateHandler(){
	if(xml.readyState == 4){
		var result = html2domparser(xml.responseText);
		this.target.innerHTML = result.show();  //THIS IS THE ONE THAT HE CAN'T FIND
	}
}

/* Function to create the ajax requester */
function createXMLHttpRequest() {
	var obj = null;
	if (window.ActiveXObject) {
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		obj = new XMLHttpRequest();
	}
	return obj;
}

/* function to parse the resulting string */
function html2domparser(str){
	/*stuffhere*/
}
}

Link to comment
https://forums.phpfreaks.com/topic/158247-how-to-get-variables-from-caller/
Share on other sites

Let me try to sketch the problem a bit more clearly:

 

I have a javascript function call Ajax

this function is a kind of object that holds an xmlhttprequest object, the url that the xml needs to load, the method it needs to use, the parameters that need to be passed to the page, and the target of the result.

 

Now I can init the function or object, set all its variables and run it.

In the ajax class is an event catcher for onreadystatechange on the xml object.

if it changes, it executes a function that checks if it's ready, and if it is, it give the result value to the target

but it can't use the target from its caller

 

how can I fix this, or is there a way to pass the variable along to the function that handels the result?

 

this is the code:

function Ajax(url, method, target, param, returnType){
this.xml = createXMLHttpRequest();
this.url = (typeof(url) != "undefined")?url:"";;
this.method = (typeof(method) != "undefined")?method:"post";
this.target = (typeof(target) != "undefined")?target:null;
this.param = (typeof(param) != "undefined")?param:"";
this.returnType = (typeof(returnType) != "undefined")?returnType:"html";

this.xml.onreadystatechange = function(){
	if(this.readyState == 4){
		var result = html2domparser(this.responseText);
		target.innerHTML = result.show();
		alert(this);
	}
}
}

/* List of class functions */
Ajax.prototype.setUrl = function(url){ alert(url);this.url = url;alert(this.url); }
Ajax.prototype.getUrl = function(){ return this.url; }
Ajax.prototype.setMethod = function(m){ if(m.toLowerCase() == "post" || m.toLowerCase() == "get"){this.method = m.toLowerCase();}else{alert("invalid method.\n can only be Post or Get.");} }
Ajax.prototype.getMethod = function(){ return this.method; }
Ajax.prototype.setTarget = function(target){ if(typeof(target) != "undefined"){ this.target = target; }else{ alert("undefined object passed as target."); } }
Ajax.prototype.getTarget = function(){ return this.target; }
Ajax.prototype.setParam = function(param){ this.param = param; }
Ajax.prototype.getParam = function(){ return this.param; }
Ajax.prototype.setReturnType = function(type){ this.returnType = type; }
Ajax.prototype.getReturnType = function(){ return this.returnType; }
Ajax.prototype.getState = function(){ return this.xml.readystate; }

Ajax.prototype.start = function(){
if(this.method == "get"){
	var urlToSend = this.url;
	if(this.param != ""){
		urlToSend += "?" + this.param;
	}
	this.xml.open("GET", urlToSend);
	this.xml.send();
}
else if(this.method == "post"){
	this.xml.open("POST", this.url);
	this.xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
	this.xml.send(this.param);
}
}

 

Please help me fix this,

 

thx in advance

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.