Jump to content

Send Back Javascript.


The Little Guy

Recommended Posts

Is it possible to send javascript back to the browser to use for processing? Please view the following examples:

 

AJAX:

function loadTeams(eventID){
var contentType = "application/x-www-form-urlencoded; charset=UTF-8";
var ajaxRequest;
try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			alert("Your Browser Doesn't support AJAX.");
			return false;
		}
	}
}
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.getElementById('teamInfo').innerHTML = ajaxRequest.responseText
	}
}
var va = '&eventID='+eventID;
ajaxRequest.open("POST", 'process/mycode.php', true);
ajaxRequest.setRequestHeader("Content-Type", contentType);
ajaxRequest.send(va);

}

 

 

PHP (mycode.php):

echo '<script>var myVar = "This is a String";</script>';

Link to comment
https://forums.phpfreaks.com/topic/117024-send-back-javascript/
Share on other sites

It is possible to process the JavaScript returned in an AJAX request, but I'm not sure the best way to do it offhand. If you look into a library such as Ext (http://www.extjs.com), there is an optional parameter passed to AJAX requests that tells it whether or not to process the JavaScript returned. As for how to actually write the processing for something like that, I'm not sure what to tell you other than looking in the code of a library that supports it.

 

The way I get around this is typically to write all my functions to be preloaded into the page, and I simply call the function within a callback of my AJAX requests using the data that was returned. Following this method, I seldom, if ever, have to actually process JavaScript that is returned. It also makes for a much more structured system in the long run... Just some thoughts ;)

 

Of course, when worse comes to worst, you can always just eval() returned JS, but that can definitely get a little dangerous, too.

Link to comment
https://forums.phpfreaks.com/topic/117024-send-back-javascript/#findComment-601893
Share on other sites

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.