Automatically inline CSS from a CSS-file (jque

2019-09-22 11:35发布

问题:

For reasons we dont need to get in to, I need to put the content of a CSS-file into the style tags of a html-page. I want to do it automatically, so I need to read the CSS file and put it's content in my style tag in my header.

Any idea how to do this?

回答1:

The best would be if you used PHP, django, ASP.net or something from this family. For PHP I would do like this:

<style id="Something">
<?php readfile("http://example.com/some/style.css"); ?>
</style>

But if you want to use jQuery, try this:

<style id="Something"></style>
<script type="text/javascript">
var request = $.ajax({
  url: "stylesheet.css",
});
request.done(function( msg ) {
  $( "style#Something" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});
</script>