Lars P
Lars P

Reputation: 932

How do I tag my CSS file with a version number that jQuery can read?

I'm making a Javascript library that comes with a CSS file. I would like for the JS to check the CSS file version.

My best idea is CSS like this

body { someproperty: "v1.3.1"; }

and then you can have JS code like this

console.log($("body").css("someproperty"))

But I'm having a hard time finding the perfect property. Is there some semi obsolete one that takes an arbitrary string or number?

Any other ideas?


@Niet the Dark Absolless gave the perfect answer to my question, but I ended up handling my CSS purely in JS:

$('head').append(my_css)

This gives me one single file to distribute, which can never get out of sync with itself. Good Times!

Upvotes: 2

Views: 305

Answers (2)

craigrs84
craigrs84

Reputation: 3064

Create a javascript file which defines the version and then load the stylesheet via javascript. Include the javascript file instead of the CSS file:

function getVersion()
{
    return '1.0';
}

$(document).ready(function()
{
    $('head').append('<link rel="stylesheet" href="test.css" type="text/css"/>');
});

Upvotes: 0

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324760

Actually, there is. Sort of.

body {content:"Hello, world!";}

Because it's not a psuedo-elment, it has no effect.

But if you then do:

console.log($("body").css("content"));

... Close enough! -- Just need to strip the quotes from the start and end ;)

console.log($("body").css("content").slice(1,-1));

Upvotes: 2

Related Questions