1

I'd like to use jQuery to grab all of the css file references from my page. Any suggestions how i could construct a query to grab them all?

As an example, stackoverflow has a head section that looks like this:

<head>

    <title>Ask a Question - Stack Overflow</title>        
    <link rel="stylesheet" href="/content/all.css?v=3647">
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="apple-itouch-icon" href="/apple-touch-icon.png">
    <link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
    <script type="text/javascript" src="/content/js/master.js?v=3567"></script>
    <script type="text/javascript">var imagePath = '/content/img/so/';</script>

</head>

and from there, i'd like to use jquery to pull out just the single css class (or multiples if they existed) to work with.

2 Answers 2

4
$.each($("link[rel='stylesheet']").attr("href"), function() {
    var href = this;
    $.get(href, function(css_data) {
        // do something with css_data
    });
});

You might need to worry about capitalization on 'stylesheet', though. Also, it may be nontrivial to extract class information from css_data -- it would come down to text processing (unless there's a plugin to deal with this kind of thing that I'm not aware of).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - exactly what I needed. It didn't like my caps on the 'stylesheet' attrib.
1

To select link element and pick the link you can use:

$("link[rel=stylesheet]").attr("href")

But I'm not sure you can lookup into and pull out css class from there.

Comments

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.