2015-12-11 123 views
1

根據定義<noscript>應禁止Javascript顯示其中的文本。HTML <noscript>標記不起作用

我用IE11和禁用JavaScript來只看到一個空白頁根本沒有消息。

<head> 
<noscript>Javascript is disabled!</noscript> 
<script>document.getElementById("demo").innerHTML="Script Enabled";</script>  
</head> 
<p id="demo"> Test</p> 

我錯過了什麼嗎?

+0

什麼是HTML圍繞這個片斷裏面? –

+0

更新了我的問題。 –

+2

'noscript'屬於我的身體部分,而不是頭部;但不幸的是,這可能無法解決您的問題。 –

回答

2

我的頁面適用於我如果javascript被禁用:它在頂部打印「javascript is disabled」消息。

但是,如果我啓用它,不會更改「測試」文本,因爲該腳本在之前執行頁面被加載。即使它已加載,也沒有<body>,因此document.getElementById將返回null

的工作調整可能是這樣的:

<!doctype html> 
<html> 
<head> 
<noscript>Javascript is disabled!</noscript> 
<script>window.onload=function(){document.getElementById("demo").innerHTML="Script Enabled";}</script> 
</head> 
<body> 
<p id="demo"> Test</p> 
</body> 
</html> 
+0

有趣。你使用鉻? –

+0

是的。經過IE11和Chrome測試。兩者都有相同的行爲,而我的解決方案在兩者中都起作用。 –

1

從上面告訴我們的情況來看,您似乎已經輸入/使用了正確的語法。

我在Chrome 47中試過了,運行良好,也許在不同的瀏覽器中試過並查看結果,或者確保實際上關閉了JavaScript。

例子:

<script> 
 
document.write("Hello World!") 
 
</script> 
 
<noscript>Your browser does not support JavaScript!</noscript>

從W3Schools的 - here

你總是可以做到這一點太:

$(document).ready(function() { 
 
    $('#noscript-description').css({ 
 
    'display' : 'none' 
 
    
 
    // Just change the none to block to see the results 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
    <body> 
 
    <p id="noscript-description">This can be seen if you <b>DO NOT</b> have JavaScript enabled.</p> 
 
    <!-- I have added in jQuery, if you never realised --> 
 
    </body> 
 
</html>

0

你應該把指令碼的標籤body標籤