
What's wrong with this picture?

// The horribly inefficient naive way:
function average( a,b ) {
var REDMASK = 0x00ff0000;
var GREENMASK = 0x0000ff00;
var BLUEMASK = 0x000000ff;
var aRed = a & REDMASK;
var aGreen = a & GREENMASK;
var aBlue = a & BLUEMASK;
var bRed = b & REDMASK;
var bGreen = b & GREENMASK;
var bBlue = b & BLUEMASK;
var aveRed = (aRed + bRed) >> 1;
var aveGreen = (aGreen + bGreen) >> 1;
var aveBlue = (aBlue + bBlue) >> 1;
return aveRed | aveGreen | aveBlue;
}
// the fast way:
MASK7BITS = 0x00fefeff;
function ave( a,b ) {
a &= MASK7BITS;
b &= MASK7BITS;
return (a+b)>>1;
}
myPage = <html>
<body>
<form action="">
... a bunch of markup here
</form>
</body>
</html>;
iframe = top.document.createElement( "iframe" );
// convert XML object to data URL
function xmlToDataURL( theXML ) {
var preamble = "data:text/html;charset=utf-8,";
var octetString = escape( theXML.toXMLString( ) );
return preamble + octetString;
}
dataURL = xmlToDataURL( myPage );
iframe.setAttribute( "src", dataURL ); // load frame
// attach the iframe to your current page
top.document.body.insertBefore( iframe ,
top.document.body.firstChild );
function toHex( n ) { return n.toString( 16 ); }// add just the right number of 'ch' characters
// to the front of string to give a new string of
// the desired final length 'dfl'
function frontPad( string, ch, dfl ) {
var array = new Array( ++dfl - string.length );
return array.join( ch ) + string;
}
function toHex( n ) {
return "0x" + frontPad( n.toString( 16 ), 0, 8);
}
toHex( 256 ) // gives "0x00000100" n = toHex( 256 ); // "0x00000100"
typeof n // "string"
isNaN( n ) // false
x = n * n; // 65536
x = n + 256 // "0x00000100256"
x = Number( n ) + 256 // 512
if ( null == arguments[ 0 ] )
return "Nothing to do";
if ( 0 == array.length )
break;
if ( array.length = 0 )
break;
This is an extraordinarily useful capability. You can create an elaborate Swing dialog (for example) containing dozens of nested widgets, then serialize the whole thing as a single XML file, capturing its state, using XMLEncoder (then deserialize it later, in another time and place, perhaps).
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("Test.xml")));
e.writeObject(new JButton("Hello, world"));
e.close();
typeof NaN == 'number' // true

function trim10 (str) {
var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for (var i = 0; i < str.length; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i >= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}
return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
function trim(a) {
return a.replace(/^ +/,"").replace(/ +$/,"");
}
<script type="application/javascript;version=1.7"/>http://localhost:7402/content.query.json?
queryType=xpath&statement=//*[contains(@pets,'dog')]http://localhost:7402/content.query.json?
queryType=xpath&statement=//*[jcr:contains(@pets,'dog')]
var serializer = new XMLSerializer( );
var str = serializer.serializeToString( document.documentElement );
var pretty = XML( str ).toXMLString( ); var serializer = new XMLSerializer();
var str = serializer.serializeToString( d );

<data>
<item>something</item>
</data>
<data>
<item>something</>
</>
| Server | Language |
| Borges | Ruby |
| Continuity | Perl |
| Lakeshore | Java |
| Seaside | smalltalk |
| Wee | Ruby |
javascript:ar=location.href.split('/');
if(ar.pop()=='')ar.pop();
u=ar.join('/');
location.href=u;
The issue is this. E4X has a dot-syntax for XML objects that allows expressions like root.x.y to obtain the y element under an x element under the root. But when an element name contains a hyphen, this syntax breaks. Consider:
var fragment =
<content>
<field>
<display-label>Please approve.</display-label>
</field>
</content>;
// Now try to access the display-label value
var value = fragment.field.display-label; // ReferenceError!
label hasn't been declared, it's undefined and unusable. If a variable named "label" does happen to exist in the current scope (e.g. you used var label in place of var value above), you won't get any error at all, since subtraction on two defined entities is always a legal production in Javascript.
// Instead of this:
var value = fragment.field.display-label; // ReferenceError!
// Do this:
var value = fragment.field["display-label"]; // "Please approve."
There's one more syntax breakage to deal with, and that involves the descendant-retrieval syntax. E.g., root..y returns a list of all y descendants under root, regardless of what level in the tree each one is at. This syntax obviously breaks down if y is something like data-item.
descendants() method.
// Instead of this:
var allLabels = fragment..display-label; // error!
// Do this:
var allLabels = fragment.descendants("display-label"); // list of nodes
Similar breakages and workarounds exist for E4X attribute syntax, the details of which are left as an exercise for the reader. 