jQuery - Get content from a link tag

1

I have a compliant link tag:

<link rel="stylesheet" type="text/css" media="all" href="../css/screen.css" />

When I upload the site, it brings me all the css that is inside this file, but when I get the content with jQuery it does not return anything to me.

console.log( $('link').html() );
(an empty string)

If I make a call only from the selector the log returns me the element:

console.log( $('link') ); // assim eu obtenho o retorno abaixo
Object[link]

My question is how to get the contents of this css with jQuery.

    
asked by anonymous 30.04.2015 / 20:45

2 answers

2

You can make a request in AJAX using $.get :

$.get('../css/screen.css', function(data){
    console.log(data);
});
    
30.04.2015 / 21:02
1

You can try something like this too, following your same line of thought to get through tag , being <link> the only one in the document as described in your question:

$.ajax({
    url: $('link').prop('href'),
    dataType: "text",
    success: function(cssText) {
        console.log(cssText);
    }
});

Of course, for this and other cases, url should call the same server, or enter that cross-domain cross domain .

    
30.04.2015 / 21:06