jQuery - Picking up content from a <link> tag

Asked

Viewed 1,640 times

1

I have a link tag as per:

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

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

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 content of this css with jQuery.

  • already tried $('link'). val(), $('link'). text() from all Empty

  • 1

    I believe you have to use AJAX. .html() returns nothing because there is no HTML inside the tag link

  • @Lucas if I use AJAX I would have to fall into PHP p/ resolve, wanted a jQuery solution or at most a pure javascript

  • 2

    Yes, you need to use Ajax. No, you don’t need to implement anything in PHP. You didn’t implement anything for the browser to get this CSS, because you think Ajax would need, which is exactly the same thing?

  • @Wineusgobboa.deOliveira can give me an example

  • @Sneepsninja It couldn’t be clearer than Lucas hehehe...

Show 1 more comment

2 answers

2


You can make an AJAX request using $.get:

$.get('../css/screen.css', function(data){
    console.log(data);
});
  • Hi Ucas I arrived in this solution too, ai o data.responseText me returns the content, thanks!

1

You can experience something like that too, following your same line of thinking of getting through the 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);
    }
});

Naturally, for this and other cases, the url should call the same server, or get into that remote server request Ajax (cross Domain).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.