0

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.

2
  • 1
    Are you looking for AJAX (or $.ajax](http://api.jquery.com/jQuery.ajax/), or [$.get)? That lets you fetch web pages (and handle their content) without navigating away from your current page. Normally Ajax requests are limited by the same-origin policy, which prevents foo.com from fetching pages on bar.com, but Chrome extensions can bypass this restriction. Commented Apr 19, 2013 at 20:19
  • I just want to parse some xml page whose link is stored in a variable. Naturally i want it's content to be stored in another variable before i parse it ( though it's not necessary ) I will look further on on the bypass thing, thank you. Commented Apr 19, 2013 at 20:24

1 Answer 1

1

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.

http://jsfiddle.net/XF8WC/

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
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

Well it means i can store my link under the 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.
@Edeph, Can you show some part of your XML and what you are trying to access?
This Where microsoft is replaced by another word.
Actually how do i use ajax inside the extension ? The log points out an error.
@Edeph, hum... forgot about the extension thing, you can read this question stackoverflow.com/questions/1955199/…, hopefull you will find a solution to run ajax there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.