Retrieving External Content into Lithium Component
What's the best practice for retrieving external content into a Lithium component? We were thinking JQuery, but what is the standard way to do this? We found a small JQuery snippet online to try to pull in the google.com home page, but it's not working in the Lithium component editor (returns partially processed HTML code, and unprocessed code). Any ideas? Below is our code.
<div id="wcmDiv"></div>
<a href="http://www.google.com" class="ajaxtrigger">load some content</a>
<@liaAddScript>
(function ($) {
var container = $('#wcmDiv');
$('.ajaxtrigger').click(function(){
doAjax($(this).attr('href'));
return false;
});
function doAjax(url){
// if it is an external URI
if(url.match('^http')){
// call YQL
var firstArg = "http://query.yahooapis.com/v1/public/yql?"+"q=select%20*%20from%20html%20where%20url%3D%22"+
encodeURIComponent(url)+"%22&format=xml'&callback=?";
alert(firstArg);
$.getJSON(firstArg,
// this function gets the data from the successful
// JSON-P call
function(data){
// if there is data, filter it and render it out
if(data.results[0]){
var data = filterData(data.results[0]);
alert(data);
container.html(data);
}
// otherwise tell the world that something went wrong
else {
var errormsg = '<p>Error: could not load the page.</p>';
container.html(errormsg);
}
}
); //end getJSON call
// if it is not an external URI, use Ajax load()
}
else {
$('#target').load(url);
}
}//end doAjax function
// filter out some nasties
function filterData(data){
data = data.replace(/<?\/body[^>]*>/g,'');
data = data.replace(/[\r|\n]+/g,'');
data = data.replace(/<--[\S\s]*?-->/g,'');
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
data = data.replace(/<script.*\/>/,'');
return data;
}
alert("here");
})(LITHIUM.jQuery);
</@liaAddScript>