Sephr
12-20-2008, 03:47 AM
This is a JavaScript snippet for a jData (http://www.eligrey.com/tag/jdata/) querying function with a callback parameter. I have also made a demo (http://code.eligrey.com/jdata/demo1.html) of this function. The code is public domain so modify it as you wish. jData works on all browsers that correctly support localStorage and window.postMessage (IE8 beta 2 has trouble with it so no support currently) so that's why jDataQuery doesn't have any code to support IE8.
var $tempMessageListener, jDataServer = 'http://jdata.eligrey.com',
jDataEnableHash = false; // Set to true to have hash value of jData frame change to current query
var jDataLoadFlags = jDataEnableHash ? '/?hash':'/';
var jDataFrame = document.createElement('iframe');
jDataFrame.setAttribute('src', jDataServer+jDataLoadFlags);
jDataFrame.setAttribute('style', 'display:none');
document.documentElement.appendChild(jDataFrame);
function jDataQuery(query, callback) {
if (callback) { $tempMessageListener = function(e){ getNextjDataResponse(e, callback); } }
else { $tempMessageListener = function(e){ getNextjDataResponse(e); } }
window.addEventListener("message", $tempMessageListener, false);
jDataFrame.contentWindow.postMessage(query, '*');
};
function getNextjDataResponse(e, callback) {
if ( e.origin == jDataServer ) { // Make sure that this is a jData response
window.removeEventListener("message", $tempMessageListener, false);
if (callback) { callback((e.data != 'null' && e.data != 'undefined')?e:undefined); }
$tempMessageListener = undefined;
}
};
Syntax: jDataQuery(query:String [..., callback:Function])
Example:
A website sets the user’s preferred search engine as JSON in preference.searchEngine:
jDataQuery("setItem('preference.searchEngine',\"{'Google':'http://www.google.com/search?q=%s'}\")")
Then another website wants to check out the JSON in preference.searchEngine if it exists:
jDataQuery("getItem('preference.searchEngine')",function(e){setSearchBox(JSON.parse(e.data))})
var $tempMessageListener, jDataServer = 'http://jdata.eligrey.com',
jDataEnableHash = false; // Set to true to have hash value of jData frame change to current query
var jDataLoadFlags = jDataEnableHash ? '/?hash':'/';
var jDataFrame = document.createElement('iframe');
jDataFrame.setAttribute('src', jDataServer+jDataLoadFlags);
jDataFrame.setAttribute('style', 'display:none');
document.documentElement.appendChild(jDataFrame);
function jDataQuery(query, callback) {
if (callback) { $tempMessageListener = function(e){ getNextjDataResponse(e, callback); } }
else { $tempMessageListener = function(e){ getNextjDataResponse(e); } }
window.addEventListener("message", $tempMessageListener, false);
jDataFrame.contentWindow.postMessage(query, '*');
};
function getNextjDataResponse(e, callback) {
if ( e.origin == jDataServer ) { // Make sure that this is a jData response
window.removeEventListener("message", $tempMessageListener, false);
if (callback) { callback((e.data != 'null' && e.data != 'undefined')?e:undefined); }
$tempMessageListener = undefined;
}
};
Syntax: jDataQuery(query:String [..., callback:Function])
Example:
A website sets the user’s preferred search engine as JSON in preference.searchEngine:
jDataQuery("setItem('preference.searchEngine',\"{'Google':'http://www.google.com/search?q=%s'}\")")
Then another website wants to check out the JSON in preference.searchEngine if it exists:
jDataQuery("getItem('preference.searchEngine')",function(e){setSearchBox(JSON.parse(e.data))})