Skip to content

WriteCaptureSupport

Noah Sloan edited this page Sep 10, 2010 · 1 revision

Implementing writeCaptureSupport

If you don't want to use jQuery and you already use another Ajax library, chances are you can easily wrap it to provide the functions writeCapture needs and save a few bytes.

writeCapture needs 3 support functions. You provide them by defining a writeCaptureSupport object:

window.writeCaptureSupport = {
	ajax: function(options) { /*...*/ },
	$: function(selector) { /* ... */ },
	replaceWith: function(selector,content) { /*...*/ }
};

ajax

ajax must provide a subset of the functionality of jQuery.ajax. The following options are used:

{
	url: url, // the url to load
	type: 'GET', // will always be GET
	dataType: "script", // will be "script" or "text"
	async: true/false,
	success: callback(text), // called on sucess
	error: callback(xhr,status,error) // called on error
}

Implementations can assume that type is always GET, but the parameter will be passed regardless. If dataType === "script", then it is assumed that the script will be loaded and executed asynchronously via script tag injection. dataType will only be set to "script" if the script being loaded is on another domain, otherwise it will be "text" and the response must not be evaluated as a script.

The success callback should be passed the text of the script (if dataType:"script", the text will not be used and can be omitted). The error callback should be passed 3 parameters, the third being the Error that occurred. The first two parameters are ignored. Either success or error must eventually be called for each call to ajax.

replaceWith

replaceWith is equivalent to jQuery's replaceWith. The key feature is that the second parameter will always be an HTML string and any script tags it contains must be executed. For selector support, see $ below.

$

Resolves a selector to a single element. At a minimum, ID based selectors must be supported. If an Element is passed, it should be returned unmodified. This should be the same function used by replaceWith to resolve the element to replace.

writeCaptureSupport.$('#foo') == document.getElementById('foo')
writeCaptureSupport.$(document.body) == document.body

This function (and replaceWith) should throw an informative error if an unsupported selector is given.

onLoad (optional)

Equivalent to jQuery's $(document).ready(), Prototype's Event.observe(window, 'load'), dojo's dojo.addOnLoad() and Moo Tool's window.addEvent('domready') or plain old window.onload = . Takes a function and calls it when the document has loaded.

This method is optional but is required to use autoAsync. If you are using nolib-support.js, which does not implement onLoad, you can implement it easily:

writeCaptureSupport.onLoad = function(fn) {
    // note that this is a poor implementation, use/copy a framework
    window.onload = fn;
};

copyAttrs (optional)

Used by proxyGetElementById to copy attributes from the proxy div to the real element. If not present, the attributes are not copied, which usually wont matter.

Clone this wiki locally