I have a link stored in a variable and i want to get it's content in another var. The page itself is a XML page. I understood that it can be done within php, but i am curious if there is any way to get it through jQuery of javascript. I use it inside a chrome extension and php is useless from what i've understood.
1 Answer
Have a look at http://api.jquery.com/jQuery.parseXML/
var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>"
,xmlDoc = $.parseXML( xml )
,$xml = $( xmlDoc )
,$title = $xml.find( "title" );
If your XML would contain a a tags, you could get them all using $xml.find('a').
"This Where microsoft is replaced by another word."
Given the new comments you posted, if you want to use Google's suggest API from the browser, you will not be able to work with XML. As far as I know, that API is not designed to be used by external tiers and it will not accept cross-domain requests. However, there is still a way to use the API, but as a result of the work around, you will have to work with a different data format.
Here, we specify youtube as the client url param, wich allows us to use JSONP to retrieve data, because the API's return format is based on the client.
For more informations, please refer to this article.
$.ajax({
url: 'http://google.com/complete/search',
data: {
client: 'youtube',
q: 'microsoft',
jsonp: 'searchCallback'
},
dataType: 'jsonp'
});
window.searchCallback = function (data) {
$.each(data[1], function (index, item) {
console.log(item[0]); //log suggestion
});
}
5 Comments
var xmland then continue just like in the example ? I'm confused about how i can actually get the data from the link stored in the var link and pass the content in another var for parsing.
foo.comfrom fetching pages onbar.com, but Chrome extensions can bypass this restriction.