JavaScript

Jump to: navigation, search
Categories » Programming » Web Programming » JavaScript » JavaScript

Contents

Functions that should be in JavaScript, but aren't

XMLHttpRequest

Cross-Browser Support

From Wikipedia:XMLHttpRequest

if( typeof XMLHttpRequest == "undefined" ) {
	XMLHttpRequest = function() {
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {};
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {};
		try { return new ActiveXObject("Msxml2.XMLHTTP"); }     catch(e) {};
		try { return new ActiveXObject("Microsoft.XMLHTTP"); }  catch(e) {};
 
		throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.");
	}
}

Then, when creating the object, you simply need

var request = new XMLHttpRequest(); // No conditionals necessary.

Caching

When using GET, caching may become an issue. One simple way of getting around this is to use POST for everything. The Wikipedia article above details other methods.

Looping through an object's properties

for (var name in myObject) {
	alert("The property " + name + " is equal to " + myObject[name]);
}

Creating HTML Elements

document.createElement('name');

Then you can set things like cellSpacing, style.border, style.backgroundColor, etc. actb.js uses this method to generate the autocomplete box.

Foreach Loops

Use this instead:

for (var x in myObject) { alert(myObject[x]); }

outerHTML

http://snipplr.com/view/5460/outerhtml-in-firefox/

Personal tools
Namespaces
Variants
Actions
Navigation
Categories
Toolbox