/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/IbKMB prereq: firefox w/ firebug installed and a server running php w/ simplexml note: i'm using a server-side proxy so we can easily make requests to an arbitrary domain usage: 1) put this code in an php file called 'proxy.php' (or whatever, but make sure the url in the javascript matches) 2) upload this file to your server 3) run it in firefox 4) look for output in console */ <?php if($_GET['run']){//wait to run until javascript loads and recursively makes a request to this file $url = 'http://query.yahooapis.com/v1/public/yql?q=show%20tables&format=xml';//arbitrary xml $sxml = simplexml_load_file($url);//just an easy way to request xml //strip off the xml declaration because the javascript XML() object expects raw xml echo str_replace('', '', $sxml->asXML()); }else{//on initial load, output the html/javascript ?> var callback = function (text) { var xml = new XML(text);//convert text to an e4x-ready xml object console.log(xml); }, url = 'proxy.php?run=true', req = new XMLHttpRequest(); req.open('GET', url, true); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { callback(req.responseText);//load up xml from proxy as text } }; req.send(null);
Tag: javascript
playing with e4x in firefox: iteratively appending xml elements
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/14GnLT prereq: firefox w/ firebug installed note: if there is no pre-existing 'child' element, using '+=' operator will append 'child' to 'parent', not 'children' 1) put this code in an html file 2) run it in firefox 3) look for output in console */ var names = ['julio','juan','jose'], xml = ; for(var i = 0; i < names.length; i++) { //check for pre-existing 'child' if(xml.children.child){//if there, append xml.children.child += ; }else{//create initial 'child' element xml.children.child = ; } } console.log(xml);
playing with e4x in firefox: iteration using for-each loop
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/y8udj prereq: firefox w/ firebug installed 1) put this code in an html file 2) run it in firefox 3) look for output in console */ var xml = ; for each (var child in xml.children.child) { console.log(child.@name); }
playing with e4x in firefox: iteration with a for loop
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/y8udj prereq: firefox w/ firebug installed 1) put this code in an html file 2) run it in firefox 3) look for output in console */ var xml = ; for (var i = 0; i < xml.children.child.length(); i++) {//note: 'parent' is not the root var name console.log(xml.children.child[i].@name); }
playing with e4x in firefox: accessing attributes
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/y8udj prereq: firefox w/ firebug installed 1) put this code in an html file 2) run it in firefox 3) look for output in console */ var xml = ; console.log(xml.children.child[0].@name);
playing with e4x in firefox: accessing xml objects using dot and bracket notation
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/y8udj prereq: firefox w/ firebug installed 1) put this in an html file and run it in firefox: 2) look for the output in the firebug console */ var h = 'html'; var text = "Here's some text"; var doc = {text}; console.log(doc);
playing with e4x in firefox: working with variables
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ref: http://bit.ly/y8udj prereq: firefox w/ firebug installed 1) put this in an html file and run it in firefox: 2) look for the output in the firebug console */ var h = 'html'; var text = "Here's some text"; var doc = {text}; console.log(doc);
playing with e4x in firefox: working w/ xml literals
/* ref: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X 1) create html file, e.g., "test.html" 2) copy paste the code below in it 3) run it in firefox w/ the firebug plugin: 4) look for the output in the firebug console */ var languages = JavaScript Python ; console.log(languages);
caja-safe roots javascript utils
var foreach = function(collection, callback){//tested: ff 3 mac if(collection.length){//array or node list for(var i = 0; i < collection.length; i++){ callback(i, collection[i]); } }else if(collection.hasOwnProperty){ for(var key in collection){ if(collection.hasOwnProperty(key)){ callback(key, collection[key]); } } }else{ throw('each() error: collection (' + collection + ') is neither an array nor an object'); } }, getElementsByClassName = function(elements, className){ var nodes = []; foreach(elements, function(i,element){ if(className === element.className){ nodes.push(element); } }); return nodes; }, hasClass = function(node, className){//tested: ff 2/3 win/mac, ie 6/7 win return new RegExp('[\b]*' + className + '[\b]*').test(node.className); },
App Engine Y!AP app that pushes updates via OpenSocial JS API
Usage:
- create App Engine app
- edit main.py to look like the code below and deploy
- create YAP app
- set app base url to yourappname.appspot.com/example
- preview your app
import wsgiref.handlers from google.appengine.ext import webapp class ExampleHandler(webapp.RequestHandler): def post(self): html = """ //ref: http://developer.yahoo.com/yap/guide/opensocial-examples.html var postActivity = function(title, body) { var params = {}; params[opensocial.Activity.Field.TITLE] = title; params[opensocial.Activity.Field.BODY] = body; var activity = opensocial.newActivity(params); opensocial.requestCreateActivity( activity, opensocial.CreateActivityPriority.LOW, function(){}); }, handleResponse = function(response){ var viewer = response.get('viewer').getData(), name = viewer.getDisplayName(); postActivity( name + ' posted an update ...', '... using OpenSocial!' ); }, getViewerData = function() { var req = opensocial.newDataRequest(); req.add(req.newFetchPersonRequest("VIEWER"), "viewer"); req.send(handleResponse); }; //this is the bare minimum code to push updates var params = {}; params[opensocial.Activity.Field.TITLE] = 'title'; params[opensocial.Activity.Field.BODY] = 'body'; var activity = opensocial.newActivity(params); opensocial.requestCreateActivity( activity, opensocial.CreateActivityPriority.LOW, function(){}); //this is a slightly enhanced update flow getViewerData(); """ self.response.headers['Content-Type'] = 'text/html' self.response.out.write(html) application = webapp.WSGIApplication( [('/example', ExampleHandler)], debug=True) def main(): wsgiref.handlers.CGIHandler().run(application) if __name__ == '__main__': main()