The Little Guy Posted July 28, 2008 Share Posted July 28, 2008 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>'; Quote Link to comment Share on other sites More sharing options...
obsidian Posted July 28, 2008 Share Posted July 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted July 28, 2008 Author Share Posted July 28, 2008 What I was doing before, was just returning a hidden field, but that takes longer to write the code for processing into a var (in my opinion) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.