0

Long time reader, first time poster.

I need to disable an external CSS stylesheet, and am planning on using the technique described in this thread.

Removing or replacing a stylesheet (a <link>) with JavaScript/jQuery

My question is, how "early" is safe to do it? I would think that as long as script to disable the link is lower in the HTML, the CSS link (though not necessarily the contents of the external stylesheet) will be in the DOM already.

But "the other guys" are saying that's not the case, and to avoid possible race conditions, you shouldn't disable the CSS until "document.ready". (But, if you wait until document.ready, content "flicks" onto the page with the old styles.)

In other words, is this code at all risky?

<head>
<title>Example</title>
<link rel="stylesheet" href="/Common/css/default.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
jQuery('link[rel=stylesheet]').prop('disabled', true);
</script>
</head>
Community
  • 1
  • 1
  • Why in god's name don't you just set the attribute yourself? – Jonathan Feb 27 '15 at 16:23
  • 1
    I'm not a fan of disabling stylesheets. Feels like the wrong solution if the problem is trying to remove unwanted styling. – j08691 Feb 27 '15 at 16:24
  • I'm inclined to agree, but I've got some constraints to work within. The pages have to look one way when viewed normally and another way when viewed inside an iFrame. The tools I have available are CSS and JS (including jQuery). Thanks for the input. – Timster Feb 27 '15 at 16:48

1 Answers1

0

I would do it as soon as possible, and not when the document has been loaded. A flicker may be better than having the styles change after the page styles have already rendered.

$('link[rel~="stylesheet"]').prop('disabled', true);
Paul Redmond
  • 3,276
  • 4
  • 32
  • 52
  • Yes, as you suspect, when the old stylesheet is removed inside the document.ready function, you can see (1) the old design, (2) the unstyled content, and (3) the new design, in rapid succession. – Timster Feb 27 '15 at 16:54