簡體   English   中英

如果禁用javascript,如何加載外部css文件

[英]how to load an external css file if javascript is disabled

如果禁用javascript,如何加載外部css文件。

應該在IE 6到9,Firefox谷歌瀏覽器,Safari

我試圖<noscript>守信<noscript>里面,但它不是在IE 7中工作

我會反過來這樣做。 始終加載包含前綴為body.jsEnabled規則的css。 然后只需通過javascript在body上添加一個類“jsEnabled”。

這種“發現能力”的方式與http://www.modernizr.com/的工作方式大致相同。

我在IE7中測試了這個方法,它可以工作。 放置<style>標簽(而不是<noscript>中的<link> <noscript>

<noscript>
<style type="text/css">
 @import url (yourexternalfile.css);

body{
background-color: purple;
}

</style>
</noscript>

編輯:

這是在IE7中工作的屏幕截圖。 替代文字

嘗試這個:

<html>
  <head>
    <script type="text/javascript"> document.write('<'+'!--'); </script>
       <link rel="stylesheet" type="text/css" href="non-js.css">
    <!-- -->  
  </head>
  <body>
      <script type="text/javascript"> document.write('<'+'!--'); </script>
       Visible when no-JS 
      <!--  --> Always visible
  </body>
</html>

哈克,但HTML是正確的。 如果啟用了JS,則將注釋開始控制標記打印到頁面中 - 然后忽略第二個開始標記,結束標記關閉注釋內容。 因此,如果啟用了JS, link標記將被注釋掉而不會被下載。

雖然<noscript> <head>不允許<noscript><head>中只允許<link> + <style> <head> ,但您也可以使用:

<link id="noscriptStyle" rel="stylesheet" type="text/css" href="my_external_css_file.css" />
<script type="text/javascript">
document.getElementById('noscriptStyle').parentNode.removeChild(document.getElementById('noscriptStyle'));
</script>

但我自己更喜歡cherouvim發布的方法

CSS文件與啟用/禁用的javascript無關...

<link rel="stylesheet" type="text/css" href="my_external_css_file.css" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM