Link to home
Start Free TrialLog in
Avatar of sscotti
sscottiFlag for United States of America

asked on

Parse String or JSON response to get a DOM element from the string / response

I am not too familiar with this.  I found a function on the Web that can make a cross domain query using jQuery and YSQL.  It seems to be working and returns everything from <body> to <body> in data.

What I want to do is actually edit the response in data so that it is equal to an element in the response.

e.g.

There is and element in the response that has this form:

 <table id="MainContentTable" cellpadding="0" cellspacing="0" width="100%">

... ....

</table>

I want the table tags and the innerHTML.

Is there a way to do that?  I'm not sure if data is simply a string, a JSON object, XML.  Seems like it should not be too hard.

// Accepts a url and a callback function to run.
function requestCrossDomain( site, callback ) {

	// If no url was passed, exit.
	if ( !site ) {
		alert('No site was passed.');
		return false;
	}

	// Take the provided url, and add it to a YQL query. Make sure you encode it!
	var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '"') + '&format=xml&callback=?';

	// Request that YSQL string, and run a callback function.
	// Pass a defined function to prevent cache-busting.
	$.getJSON( yql, cbFunc );

	function cbFunc(data) {
	// If we have something to work with...
	if ( data.results[0] ) {
		// Strip out all script tags, for security reasons.
		// BE VERY CAREFUL. This helps, but we should do more.
		data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
		// If the user passed a callback, and it
		// is a function, call it, and send through the data var.
		if ( typeof callback === 'function') {
			callback(data);
		}
	}
	// Else, Maybe we requested a site that doesn't exist, and nothing returned.
	else throw new Error('Nothing returned from getJSON.');
	}
}

Open in new window

Avatar of KiasChaos83
KiasChaos83
Flag of Australia image

You could do

alert($("#MainContentTable", $(data.results[0])).html());
Avatar of leakim971
For the cbFunc function use something like this :


function cbFunc(data) {
		// If we have something to work with...
		if ( data.results[0] ) {
			// Strip out all script tags, for security reasons.
			// BE VERY CAREFUL. This helps, but we should do more.
			data = "<div>" + data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '') + "</div>";
			// If the user passed a callback, and it
			// is a function, call it, and send through the data var.
			if ( typeof callback === 'function') {
				callback(data);
			}
			$("#putTableHere").append( $(data).find("table[id='MainContentTable']") );
		}
		// Else, Maybe we requested a site that doesn't exist, and nothing returned.
		else throw new Error('Nothing returned from getJSON.');
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
work too : $("#putTableHere").append( $("#MainContentTable", data) );
function cbFunc(data) {
		// If we have something to work with...
		if ( data.results[0] ) {
			// Strip out all script tags, for security reasons.
			// BE VERY CAREFUL. This helps, but we should do more.
			data = "<div>" + data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '') + "</div>";
			// If the user passed a callback, and it
			// is a function, call it, and send through the data var.
			if ( typeof callback === 'function') {
				callback(data);
			}
			$("#putTableHere").append( $("#MainContentTable", data));
		}
		// Else, Maybe we requested a site that doesn't exist, and nothing returned.
		else throw new Error('Nothing returned from getJSON.');
	}

Open in new window

Thanks a lot for the points!